Fix for a significant error in naming conventions, with tests.
This commit is contained in:
parent
1860790027
commit
27044d6405
|
@ -261,7 +261,7 @@
|
|||
" "
|
||||
(map
|
||||
#(apply str (cons (Character/toUpperCase (first %)) (rest %)))
|
||||
(s/split s #"[ \t\r\n]+")))
|
||||
(s/split s #"[^a-zA-Z0-9]+")))
|
||||
s))
|
||||
|
||||
|
||||
|
@ -272,30 +272,36 @@
|
|||
|
||||
(defn safe-name
|
||||
"Return a safe name for the object `o`, given the specified `convention`.
|
||||
`o` is expected to be either a string or an element."
|
||||
`o` is expected to be either a string or an element. Recognised values for
|
||||
`convention` are: #{:c :c-sharp :java :sql}"
|
||||
([o]
|
||||
(cond
|
||||
(element? o)
|
||||
(safe-name (:name (:attrs o)))
|
||||
true
|
||||
(s/replace (str o) #"[^a-zA-Z0-9-]" "")))
|
||||
(element? o)
|
||||
(safe-name (:name (:attrs o)))
|
||||
true
|
||||
(s/replace (str o) #"[^a-zA-Z0-9-]" "")))
|
||||
([o convention]
|
||||
(cond
|
||||
(and (entity? o) (= convention :sql))
|
||||
;; if it's an entity, it's permitted to have a different table name
|
||||
;; from its entity name. This isn't actually likely, but...
|
||||
(safe-name (or (-> o :attrs :table) (-> o :attrs :name)) :sql)
|
||||
(element? o)
|
||||
(safe-name (:name (:attrs o)) convention)
|
||||
true
|
||||
(let [string (str o)]
|
||||
(case convention
|
||||
(:sql :c) (s/replace string #"[^a-zA-Z0-9_]" "_")
|
||||
:c-sharp (s/replace (capitalise string) #"[^a-zA-Z0-9]" "")
|
||||
:java (let
|
||||
[camel (s/replace (capitalise string) #"[^a-zA-Z0-9]" "")]
|
||||
(apply str (cons (Character/toLowerCase (first camel)) (rest camel))))
|
||||
(safe-name string))))))
|
||||
(and (entity? o) (= convention :sql))
|
||||
;; if it's an entity, it's permitted to have a different table name
|
||||
;; from its entity name. This isn't actually likely, but...
|
||||
(safe-name (or (-> o :attrs :table) (-> o :attrs :name)) :sql)
|
||||
(and (property? o) (= convention :sql))
|
||||
;; if it's a property, it's entitle to have a different column name
|
||||
;; from its property name.
|
||||
(safe-name (or (-> o :attrs :column) (-> o :attrs :name)) :sql)
|
||||
(element? o)
|
||||
(safe-name (:name (:attrs o)) convention)
|
||||
true
|
||||
(let [string (str o)
|
||||
capitalised (capitalise string)]
|
||||
(case convention
|
||||
(:sql :c) (s/replace string #"[^a-zA-Z0-9_]" "_")
|
||||
:c-sharp (s/replace capitalised #"[^a-zA-Z0-9]" "")
|
||||
:java (let
|
||||
[camel (s/replace capitalised #"[^a-zA-Z0-9]" "")]
|
||||
(apply str (cons (Character/toLowerCase (first camel)) (rest camel))))
|
||||
(safe-name string))))))
|
||||
|
||||
;; (safe-name "address-id" :sql)
|
||||
;; (safe-name {:tag :property :attrs {:name "address-id"}} :sql)
|
||||
|
|
|
@ -506,3 +506,69 @@
|
|||
(is (= false (unique-link? e1 e2)) "There are two logical links, three link properties, between e1 and e2")
|
||||
(is (= true (unique-link? e2 e3)) "There is only one link between e2 and e3"))))
|
||||
|
||||
(deftest capitalise-tests
|
||||
(testing "capitalise"
|
||||
(is (= (capitalise "the quick brown fox jumped over the lazy dog") "The Quick Brown Fox Jumped Over The Lazy Dog"))))
|
||||
|
||||
(deftest safe-name-tests
|
||||
(testing "safe-name"
|
||||
(let [e1 {:tag :entity
|
||||
:attrs {:name "canvass-teams" :table "team"}
|
||||
:content [{:tag :key
|
||||
:content [{:tag :property
|
||||
:attrs {:name "id" :type "integer" :distinct "system"}}]}
|
||||
{:tag :property
|
||||
:attrs {:name "members" :type "link" :entity "canvassers"}}
|
||||
{:tag :property
|
||||
:attrs {:name "organisers" :type "link" :entity "canvassers"}}]}
|
||||
p1 {:tag :property
|
||||
:attrs {:name "id" :type "integer" :distinct "system"}}
|
||||
p2 {:tag :property
|
||||
:attrs {:name "with_underscore" :column "with-hyphen" :type "integer"}}]
|
||||
(is
|
||||
(= (safe-name "the quick brown fox jumped over the lazy dog")
|
||||
"thequickbrownfoxjumpedoverthelazydog")
|
||||
"absent a convention, spaces are suppressed")
|
||||
(is
|
||||
(= (safe-name "the quick brown fox jumped over the lazy dog" :c)
|
||||
"the_quick_brown_fox_jumped_over_the_lazy_dog")
|
||||
"in :c convention, spaces are replaced with underscores")
|
||||
(is
|
||||
(= (safe-name "the quick brown fox jumped over the lazy dog" :c-sharp)
|
||||
"TheQuickBrownFoxJumpedOverTheLazyDog")
|
||||
"in :c-sharp convention spaces are suppressed and all words camel cased")
|
||||
(is
|
||||
(= (safe-name "the quick brown fox jumped over the lazy dog" :java)
|
||||
"theQuickBrownFoxJumpedOverTheLazyDog")
|
||||
"in :java convention spaces are suppressed and embedded words camel cased")
|
||||
(is
|
||||
(= (safe-name "the quick brown fox jumped over the lazy dog" :sql)
|
||||
"the_quick_brown_fox_jumped_over_the_lazy_dog")
|
||||
"in :sql convention, spaces are replaced with underscores")
|
||||
(is (= (safe-name e1) "canvass-teams"))
|
||||
(is (= (safe-name e1 :c) "canvass_teams")
|
||||
"In :c convention, hyphen is replaced by underscore")
|
||||
(is (= (safe-name e1 :c-sharp) "CanvassTeams")
|
||||
"In :c-sharp convention, hyphen is suppressed and words capitalised")
|
||||
(is (= (safe-name e1 :java) "canvassTeams")
|
||||
"In :java convention, hyphen is suppressed and embedded words capitalised")
|
||||
(is (= (safe-name e1 :sql) "team")
|
||||
"In :sql convention, the :table attribute is preferred")
|
||||
(is (= (safe-name p1) "id"))
|
||||
(is (= (safe-name p1 :c) "id"))
|
||||
(is (= (safe-name p1 :c-sharp) "Id"))
|
||||
(is (= (safe-name p1 :java) "id"))
|
||||
(is (= (safe-name p1 :sql) "id"))
|
||||
(is (= (safe-name p2) "withunderscore")
|
||||
"Absent a convention, underscore is not considered safe")
|
||||
(is (= (safe-name p2 :c) "with_underscore")
|
||||
"In :c convention, underscore is considered safe")
|
||||
(is (= (safe-name p2 :c-sharp) "WithUnderscore")
|
||||
"In :c-sharp convention, initial letters are capialised and underscore is suppressed")
|
||||
(is (= (safe-name p2 :java) "withUnderscore")
|
||||
"In :java convention, underscore is suppressed and embedded words capitalised")
|
||||
(is (= (safe-name p2 :sql) "with_hyphen")
|
||||
"In :sql convention, the column-name variant is preferred, and hyphens replaced with underscores"))))
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue