[report] rewrite gnc:report-template-has-unique-name?

This commit is contained in:
Christopher Lam 2019-04-08 22:56:25 +08:00
parent ad51c5e00a
commit eab53d44b3
2 changed files with 19 additions and 10 deletions

View File

@ -489,16 +489,12 @@ not found.")))
;; to test if the new name is unique among the existting custom reports.
;; If not the calling function can prevent the name from being updated.
(define (gnc:report-template-has-unique-name? templ-guid new-name)
(let* ((unique? #t))
(if new-name
(hash-for-each
(lambda (id rec)
(if (and (not (equal? templ-guid id))
(gnc:report-template-is-custom/template-guid? id)
(equal? new-name (gnc:report-template-name rec)))
(set! unique? #f)))
*gnc:_report-templates_*))
unique?))
(or (not new-name)
(not (any
(lambda (tmpl)
(and (not (equal? (car tmpl) templ-guid))
(equal? (gnc:report-template-name (cdr tmpl)) new-name)))
(gnc:custom-report-templates-list)))))
;; Generate a unique custom template name using the given string as a base
;; If this string already exists as a custom template name, a

View File

@ -196,6 +196,19 @@
(test-equal "(gnc:is-custom-report-type report) #f"
#f
(gnc:is-custom-report-type report))
(test-assert "gnc:report-template-has-unique-name? #t"
(gnc:report-template-has-unique-name?
"54c2fc051af64a08ba2334c2e9179e24"
"Test Report Templatx"))
(test-assert "gnc:report-template-has-unique-name? #f"
(not
(gnc:report-template-has-unique-name?
"54c2fc051af64a08ba2334c2e9179e24"
"Test Report Template")))
(test-assert "gnc:report-template-has-unique-name? #t"
(gnc:report-template-has-unique-name?
"54c2fc051af64a08ba2334c2e9179e24"
#f))
(test-assert "gnc:report-serialize = string"
(string?
(gnc:report-serialize report)))))