mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
String.pluralized, String.singularized
This commit is contained in:
1 parent
5113d08ce3
commit
d2c33715d0
5 files changed
+490
No files matched your search
@@ -540,6 +540,10 @@
|
||||
5698AD3B1DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */; };
|
||||
569BBA27228DE51800478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA20228DE51800478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
569BBA28228DE51800478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA20228DE51800478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
569BBA3622905FFA00478429 /* InflectionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA3522905FFA00478429 /* InflectionsTests.swift */; };
|
||||
569BBA3722905FFA00478429 /* InflectionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA3522905FFA00478429 /* InflectionsTests.swift */; };
|
||||
569BBA4622906A8200478429 /* InflectionsTests.json in Resources */ = {isa = PBXBuildFile; fileRef = 569BBA4522906A8200478429 /* InflectionsTests.json */; };
|
||||
569BBA4722906A8200478429 /* InflectionsTests.json in Resources */ = {isa = PBXBuildFile; fileRef = 569BBA4522906A8200478429 /* InflectionsTests.json */; };
|
||||
569C1EB51CF07DDD0042627B /* SchedulingWatchdogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */; };
|
||||
569D6DDE220EF9E100A058A9 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */; };
|
||||
569D6DDF220EF9E100A058A9 /* SQLInterpolation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */; };
|
||||
@@ -1158,6 +1162,8 @@
|
||||
5698AD201DABAEFA0056AF8C /* FTS5WrapperTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5WrapperTokenizer.swift; sourceTree = "<group>"; };
|
||||
5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5CustomTokenizer.swift; sourceTree = "<group>"; };
|
||||
569BBA20228DE51800478429 /* AssociationPrefetchingFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
569BBA3522905FFA00478429 /* InflectionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InflectionsTests.swift; sourceTree = "<group>"; };
|
||||
569BBA4522906A8200478429 /* InflectionsTests.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = InflectionsTests.json; sourceTree = "<group>"; };
|
||||
569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SchedulingWatchdogTests.swift; sourceTree = "<group>"; };
|
||||
569D6DDD220EF9E100A058A9 /* SQLInterpolation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLInterpolation.swift; sourceTree = "<group>"; };
|
||||
569EF0E1200D2D8400A9FA45 /* DatabaseRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseRegion.swift; sourceTree = "<group>"; };
|
||||
@@ -1775,6 +1781,8 @@
|
||||
5605F1861C69111300235C62 /* DatabaseRegionTests.swift */,
|
||||
56AF746A1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift */,
|
||||
56EB0AB11BCD787300A3DC55 /* DataMemoryTests.swift */,
|
||||
569BBA4522906A8200478429 /* InflectionsTests.json */,
|
||||
569BBA3522905FFA00478429 /* InflectionsTests.swift */,
|
||||
56043299228F00C2009D3FE2 /* OrderedDictionaryTests.swift */,
|
||||
56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */,
|
||||
56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */,
|
||||
@@ -2475,6 +2483,7 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
569BBA4722906A8200478429 /* InflectionsTests.json in Resources */,
|
||||
568735A21CEDE16C009B9116 /* Betty.jpeg in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -2483,6 +2492,7 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
569BBA4622906A8200478429 /* InflectionsTests.json in Resources */,
|
||||
56D496C21D81374C008276D7 /* Betty.jpeg in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -2873,6 +2883,7 @@
|
||||
56FDECE31BB32DFD009AD709 /* RowFromStatementTests.swift in Sources */,
|
||||
56DE7B121C3D93ED00861EB8 /* StatementArgumentsTests.swift in Sources */,
|
||||
56AF746F1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */,
|
||||
569BBA3722905FFA00478429 /* InflectionsTests.swift in Sources */,
|
||||
5672DE6A1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */,
|
||||
5698AC4D1DA2D48A0056AF8C /* FTS3RecordTests.swift in Sources */,
|
||||
56B15D0B1CD4C35100A24C8B /* FetchedRecordsControllerTests.swift in Sources */,
|
||||
@@ -3068,6 +3079,7 @@
|
||||
56DAA2D21DE99DAB006E10C8 /* DatabaseCursorTests.swift in Sources */,
|
||||
56D496AE1D813345008276D7 /* DatabasePoolCollationTests.swift in Sources */,
|
||||
5698AC491DA2D48A0056AF8C /* FTS3RecordTests.swift in Sources */,
|
||||
569BBA3622905FFA00478429 /* InflectionsTests.swift in Sources */,
|
||||
56D496931D81316E008276D7 /* FetchableRecordTests.swift in Sources */,
|
||||
56D496881D81316E008276D7 /* DatabaseValueConversionTests.swift in Sources */,
|
||||
56D496BA1D813482008276D7 /* DatabaseQueueSchemaCacheTests.swift in Sources */,
|
||||
|
||||
@@ -7,4 +7,189 @@ extension String {
|
||||
}
|
||||
return String(first).uppercased() + dropFirst()
|
||||
}
|
||||
|
||||
var pluralized: String {
|
||||
return Inflections.default.pluralize(self)
|
||||
}
|
||||
|
||||
var singularized: String {
|
||||
return Inflections.default.singularize(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct Inflections {
|
||||
private var pluralizeRules: [(NSRegularExpression, String)] = []
|
||||
private var singularizeRules: [(NSRegularExpression, String)] = []
|
||||
private var uncountablesRegularExpressions: [String: NSRegularExpression] = [:]
|
||||
var uncountables: Set<String> {
|
||||
return Set(uncountablesRegularExpressions.keys)
|
||||
}
|
||||
|
||||
public init() {
|
||||
}
|
||||
|
||||
public mutating func plural(_ pattern: String, options: NSRegularExpression.Options = [.caseInsensitive], _ template: String) {
|
||||
let reg = try! NSRegularExpression(pattern: pattern, options: options)
|
||||
pluralizeRules.append((reg, template))
|
||||
}
|
||||
|
||||
public mutating func singular(_ pattern: String, options: NSRegularExpression.Options = [.caseInsensitive], _ template: String) {
|
||||
let reg = try! NSRegularExpression(pattern: pattern, options: options)
|
||||
singularizeRules.append((reg, template))
|
||||
}
|
||||
|
||||
public mutating func uncountable(_ words: String...) {
|
||||
for word in words {
|
||||
let escWord = NSRegularExpression.escapedPattern(for: word)
|
||||
uncountablesRegularExpressions[word] = try! NSRegularExpression(pattern: "\\b\(escWord)\\Z", options: [.caseInsensitive])
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func irregular(_ singular: String, _ plural: String) {
|
||||
let s0 = singular.first!
|
||||
let srest = singular.dropFirst()
|
||||
|
||||
let p0 = plural.first!
|
||||
let prest = plural.dropFirst()
|
||||
|
||||
if s0.uppercased() == p0.uppercased() {
|
||||
self.plural("(\(s0))\(srest)$", options: [.caseInsensitive], "$1\(prest)")
|
||||
self.plural("(\(p0))\(prest)$", options: [.caseInsensitive], "$1\(prest)")
|
||||
|
||||
self.singular("(\(s0))\(srest)$", options: [.caseInsensitive], "$1\(srest)")
|
||||
self.singular("(\(p0))\(prest)$", options: [.caseInsensitive], "$1\(srest)")
|
||||
} else {
|
||||
self.plural("\(s0.uppercased())(?i)\(srest)$", options: [], p0.uppercased() + prest)
|
||||
self.plural("\(s0.lowercased())(?i)\(srest)$", options: [], p0.lowercased() + prest)
|
||||
self.plural("\(p0.uppercased())(?i)\(prest)$", options: [], p0.uppercased() + prest)
|
||||
self.plural("\(p0.lowercased())(?i)\(prest)$", options: [], p0.lowercased() + prest)
|
||||
|
||||
self.singular("\(s0.uppercased())(?i)\(srest)$", options: [], s0.uppercased() + srest)
|
||||
self.singular("\(s0.lowercased())(?i)\(srest)$", options: [], s0.lowercased() + srest)
|
||||
self.singular("\(p0.uppercased())(?i)\(prest)$", options: [], s0.uppercased() + srest)
|
||||
self.singular("\(p0.lowercased())(?i)\(prest)$", options: [], s0.lowercased() + srest)
|
||||
}
|
||||
}
|
||||
|
||||
func isUncountable(_ string: String) -> Bool {
|
||||
let range = NSRange(location: 0, length: string.utf16.count)
|
||||
for (_, reg) in uncountablesRegularExpressions {
|
||||
if reg.firstMatch(in: string, options: [], range: range) != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public func pluralize(_ string: String) -> String {
|
||||
if isUncountable(string) {
|
||||
return string
|
||||
}
|
||||
return inflect(string, with: pluralizeRules)
|
||||
}
|
||||
|
||||
public func singularize(_ string: String) -> String {
|
||||
if isUncountable(string) {
|
||||
return string
|
||||
}
|
||||
return inflect(string, with: singularizeRules)
|
||||
}
|
||||
|
||||
private func inflect(_ string: String, with rules: [(NSRegularExpression, String)]) -> String {
|
||||
if string.isEmpty {
|
||||
return string
|
||||
}
|
||||
let range = NSRange(location: 0, length: string.utf16.count)
|
||||
for (reg, template) in rules.reversed() {
|
||||
let result = NSMutableString(string: string)
|
||||
let count = reg.replaceMatches(in: result, options: [], range: range, withTemplate: template)
|
||||
if count > 0 {
|
||||
return String(result)
|
||||
}
|
||||
}
|
||||
return string
|
||||
}
|
||||
}
|
||||
|
||||
extension Inflections {
|
||||
static var `default`: Inflections = {
|
||||
// Defines the standard inflection rules. These are the starting point
|
||||
// for new projects and are not considered complete. The current set of
|
||||
// inflection rules is frozen. This means, we do not change them to
|
||||
// become more complete. This is a safety measure to keep existing
|
||||
// applications from breaking.
|
||||
|
||||
var inflections = Inflections()
|
||||
|
||||
inflections.plural("$", "s")
|
||||
inflections.plural("s$", "s")
|
||||
inflections.plural("^(ax|test)is$", "$1es")
|
||||
inflections.plural("(octop|vir)us$", "$1i")
|
||||
inflections.plural("(octop|vir)i$", "$1i")
|
||||
inflections.plural("(alias|status)$", "$1es")
|
||||
inflections.plural("(bu)s$", "$1ses")
|
||||
inflections.plural("(buffal|tomat)o$", "$1oes")
|
||||
inflections.plural("([ti])um$", "$1a")
|
||||
inflections.plural("([ti])a$", "$1a")
|
||||
inflections.plural("sis$", "ses")
|
||||
inflections.plural("(?:([^f])fe|([lr])f)$", "$1$2ves")
|
||||
inflections.plural("(hive)$", "$1s")
|
||||
inflections.plural("([^aeiouy]|qu)y$", "$1ies")
|
||||
inflections.plural("(x|ch|ss|sh)$", "$1es")
|
||||
inflections.plural("(matr|vert|ind)(?:ix|ex)$", "$1ices")
|
||||
inflections.plural("^(m|l)ouse$", "$1ice")
|
||||
inflections.plural("^(m|l)ice$", "$1ice")
|
||||
inflections.plural("^(ox)$", "$1en")
|
||||
inflections.plural("^(oxen)$", "$1")
|
||||
inflections.plural("(quiz)$", "$1zes")
|
||||
|
||||
inflections.singular("s$", "")
|
||||
inflections.singular("(ss)$", "$1")
|
||||
inflections.singular("(n)ews$", "$1ews")
|
||||
inflections.singular("([ti])a$", "$1um")
|
||||
inflections.singular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$", "$1sis")
|
||||
inflections.singular("(^analy)(sis|ses)$", "$1sis")
|
||||
inflections.singular("([^f])ves$", "$1fe")
|
||||
inflections.singular("(hive)s$", "$1")
|
||||
inflections.singular("(tive)s$", "$1")
|
||||
inflections.singular("([lr])ves$", "$1f")
|
||||
inflections.singular("([^aeiouy]|qu)ies$", "$1y")
|
||||
inflections.singular("(s)eries$", "$1eries")
|
||||
inflections.singular("(m)ovies$", "$1ovie")
|
||||
inflections.singular("(x|ch|ss|sh)es$", "$1")
|
||||
inflections.singular("^(m|l)ice$", "$1ouse")
|
||||
inflections.singular("(bus)(es)?$", "$1")
|
||||
inflections.singular("(o)es$", "$1")
|
||||
inflections.singular("(shoe)s$", "$1")
|
||||
inflections.singular("(cris|test)(is|es)$", "$1is")
|
||||
inflections.singular("^(a)x[ie]s$", "$1xis")
|
||||
inflections.singular("(octop|vir)(us|i)$", "$1us")
|
||||
inflections.singular("(alias|status)(es)?$", "$1")
|
||||
inflections.singular("^(ox)en", "$1")
|
||||
inflections.singular("(vert|ind)ices$", "$1ex")
|
||||
inflections.singular("(matr)ices$", "$1ix")
|
||||
inflections.singular("(quiz)zes$", "$1")
|
||||
inflections.singular("(database)s$", "$1")
|
||||
|
||||
inflections.uncountable(
|
||||
"equipment",
|
||||
"information",
|
||||
"rice",
|
||||
"money",
|
||||
"species",
|
||||
"series",
|
||||
"fish",
|
||||
"sheep",
|
||||
"jeans",
|
||||
"police")
|
||||
|
||||
inflections.irregular("person", "people")
|
||||
inflections.irregular("man", "men")
|
||||
inflections.irregular("child", "children")
|
||||
inflections.irregular("sex", "sexes")
|
||||
inflections.irregular("move", "moves")
|
||||
inflections.irregular("zombie", "zombies")
|
||||
|
||||
return inflections
|
||||
}()
|
||||
}
|
||||
@@ -335,6 +335,10 @@
|
||||
5698AD3A1DABAF4A0056AF8C /* FTS5CustomTokenizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */; };
|
||||
569BBA2B228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA29228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
569BBA2C228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA29228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift */; };
|
||||
569BBA40229065CF00478429 /* InflectionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA3E229065CF00478429 /* InflectionsTests.swift */; };
|
||||
569BBA41229065CF00478429 /* InflectionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569BBA3E229065CF00478429 /* InflectionsTests.swift */; };
|
||||
569BBA43229066CB00478429 /* InflectionsTests.json in Resources */ = {isa = PBXBuildFile; fileRef = 569BBA42229066CB00478429 /* InflectionsTests.json */; };
|
||||
569BBA44229066CB00478429 /* InflectionsTests.json in Resources */ = {isa = PBXBuildFile; fileRef = 569BBA42229066CB00478429 /* InflectionsTests.json */; };
|
||||
569EF0E6200D37FD00A9FA45 /* DatabaseRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569EF0E5200D37FC00A9FA45 /* DatabaseRegion.swift */; };
|
||||
569EF0E7200D37FD00A9FA45 /* DatabaseRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 569EF0E5200D37FC00A9FA45 /* DatabaseRegion.swift */; };
|
||||
56A4CDB31D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */; };
|
||||
@@ -898,6 +902,8 @@
|
||||
5698AD201DABAEFA0056AF8C /* FTS5WrapperTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5WrapperTokenizer.swift; sourceTree = "<group>"; };
|
||||
5698AD341DABAF4A0056AF8C /* FTS5CustomTokenizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTS5CustomTokenizer.swift; sourceTree = "<group>"; };
|
||||
569BBA29228DE53200478429 /* AssociationPrefetchingFetchableRecordTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociationPrefetchingFetchableRecordTests.swift; sourceTree = "<group>"; };
|
||||
569BBA3E229065CF00478429 /* InflectionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InflectionsTests.swift; sourceTree = "<group>"; };
|
||||
569BBA42229066CB00478429 /* InflectionsTests.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = InflectionsTests.json; sourceTree = "<group>"; };
|
||||
569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SchedulingWatchdogTests.swift; sourceTree = "<group>"; };
|
||||
569EF0E5200D37FC00A9FA45 /* DatabaseRegion.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseRegion.swift; sourceTree = "<group>"; };
|
||||
56A238131B9C74A90082EB20 /* DatabaseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseTests.swift; sourceTree = "<group>"; };
|
||||
@@ -1462,6 +1468,8 @@
|
||||
5605F1861C69111300235C62 /* DatabaseRegionTests.swift */,
|
||||
56AF746A1D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift */,
|
||||
56EB0AB11BCD787300A3DC55 /* DataMemoryTests.swift */,
|
||||
569BBA42229066CB00478429 /* InflectionsTests.json */,
|
||||
569BBA3E229065CF00478429 /* InflectionsTests.swift */,
|
||||
56043295228F00A9009D3FE2 /* OrderedDictionaryTests.swift */,
|
||||
56FF45551D2CDA5200F21EF9 /* RecordUniqueIndexTests.swift */,
|
||||
56A4CDAF1D4234B200B1A9B9 /* SQLExpressionLiteralTests.swift */,
|
||||
@@ -1961,6 +1969,7 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
569BBA44229066CB00478429 /* InflectionsTests.json in Resources */,
|
||||
F3BA80CE1CFB2FDE003DC1BA /* Betty.jpeg in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -1976,6 +1985,7 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
569BBA43229066CB00478429 /* InflectionsTests.json in Resources */,
|
||||
F3BA80CD1CFB2FDD003DC1BA /* Betty.jpeg in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -2203,6 +2213,7 @@
|
||||
F3BA80CF1CFB2FEA003DC1BA /* SchedulingWatchdogTests.swift in Sources */,
|
||||
F3BA80E71CFB3016003DC1BA /* RowCopiedFromStatementTests.swift in Sources */,
|
||||
F3BA812A1CFB3063003DC1BA /* RecordSubClassTests.swift in Sources */,
|
||||
569BBA40229065CF00478429 /* InflectionsTests.swift in Sources */,
|
||||
5653EB7F20961FB200F46237 /* AssociationChainSQLTests.swift in Sources */,
|
||||
6340BF871E5E3F7900832805 /* RecordPersistenceConflictPolicy.swift in Sources */,
|
||||
56AF74721D41FB9C005E9FF3 /* DatabaseValueConvertibleEscapingTests.swift in Sources */,
|
||||
@@ -2528,6 +2539,7 @@
|
||||
F3BA80D31CFB2FF4003DC1BA /* MutablePersistableRecordTests.swift in Sources */,
|
||||
562393751DEE104400A6B01F /* MapCursorTests.swift in Sources */,
|
||||
F3BA80D01CFB2FEC003DC1BA /* SchedulingWatchdogTests.swift in Sources */,
|
||||
569BBA41229065CF00478429 /* InflectionsTests.swift in Sources */,
|
||||
5653EB7E20961FB200F46237 /* AssociationChainSQLTests.swift in Sources */,
|
||||
F3BA80EC1CFB3017003DC1BA /* RowCopiedFromStatementTests.swift in Sources */,
|
||||
F3BA80D41CFB2FF4003DC1BA /* PersistableRecordTests.swift in Sources */,
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
{
|
||||
"SingularToPlural": {
|
||||
"search" : "searches",
|
||||
"switch" : "switches",
|
||||
"fix" : "fixes",
|
||||
"box" : "boxes",
|
||||
"process" : "processes",
|
||||
"address" : "addresses",
|
||||
"case" : "cases",
|
||||
"stack" : "stacks",
|
||||
"wish" : "wishes",
|
||||
"fish" : "fish",
|
||||
"jeans" : "jeans",
|
||||
"funky jeans" : "funky jeans",
|
||||
"my money" : "my money",
|
||||
|
||||
"category" : "categories",
|
||||
"query" : "queries",
|
||||
"ability" : "abilities",
|
||||
"agency" : "agencies",
|
||||
"movie" : "movies",
|
||||
|
||||
"archive" : "archives",
|
||||
|
||||
"index" : "indices",
|
||||
|
||||
"wife" : "wives",
|
||||
"safe" : "saves",
|
||||
"half" : "halves",
|
||||
|
||||
"move" : "moves",
|
||||
|
||||
"salesperson" : "salespeople",
|
||||
"person" : "people",
|
||||
|
||||
"spokesman" : "spokesmen",
|
||||
"man" : "men",
|
||||
"woman" : "women",
|
||||
|
||||
"basis" : "bases",
|
||||
"diagnosis" : "diagnoses",
|
||||
"diagnosis_a" : "diagnosis_as",
|
||||
|
||||
"datum" : "data",
|
||||
"medium" : "media",
|
||||
"stadium" : "stadia",
|
||||
"analysis" : "analyses",
|
||||
"my_analysis" : "my_analyses",
|
||||
|
||||
"node_child" : "node_children",
|
||||
"child" : "children",
|
||||
|
||||
"experience" : "experiences",
|
||||
"day" : "days",
|
||||
|
||||
"comment" : "comments",
|
||||
"foobar" : "foobars",
|
||||
"newsletter" : "newsletters",
|
||||
|
||||
"old_news" : "old_news",
|
||||
"news" : "news",
|
||||
|
||||
"series" : "series",
|
||||
"miniseries" : "miniseries",
|
||||
"species" : "species",
|
||||
|
||||
"quiz" : "quizzes",
|
||||
|
||||
"perspective" : "perspectives",
|
||||
|
||||
"ox" : "oxen",
|
||||
"photo" : "photos",
|
||||
"buffalo" : "buffaloes",
|
||||
"tomato" : "tomatoes",
|
||||
"dwarf" : "dwarves",
|
||||
"elf" : "elves",
|
||||
"information" : "information",
|
||||
"equipment" : "equipment",
|
||||
"bus" : "buses",
|
||||
"status" : "statuses",
|
||||
"status_code" : "status_codes",
|
||||
"mouse" : "mice",
|
||||
|
||||
"louse" : "lice",
|
||||
"house" : "houses",
|
||||
"octopus" : "octopi",
|
||||
"virus" : "viri",
|
||||
"alias" : "aliases",
|
||||
"portfolio" : "portfolios",
|
||||
|
||||
"vertex" : "vertices",
|
||||
"matrix" : "matrices",
|
||||
"matrix_fu" : "matrix_fus",
|
||||
|
||||
"axis" : "axes",
|
||||
"taxi" : "taxis",
|
||||
"testis" : "testes",
|
||||
"crisis" : "crises",
|
||||
|
||||
"rice" : "rice",
|
||||
"shoe" : "shoes",
|
||||
|
||||
"horse" : "horses",
|
||||
"prize" : "prizes",
|
||||
"edge" : "edges",
|
||||
|
||||
"database" : "databases",
|
||||
|
||||
"slice" : "slices",
|
||||
"police" : "police"
|
||||
},
|
||||
|
||||
"Irregularities": {
|
||||
"person" : "people",
|
||||
"man" : "men",
|
||||
"child" : "children",
|
||||
"sex" : "sexes",
|
||||
"move" : "moves",
|
||||
"cow" : "kine",
|
||||
"zombie" : "zombies",
|
||||
"genus" : "genera"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
@testable import GRDBCustomSQLite
|
||||
#else
|
||||
@testable import GRDB
|
||||
#endif
|
||||
|
||||
// https://github.com/rails/rails/blob/v6.0.0.rc1/activesupport/test/inflector_test.rb
|
||||
class InflectionsTests: GRDBTestCase {
|
||||
private var originalInflections: Inflections?
|
||||
|
||||
override func setUp() {
|
||||
// Dups the singleton before each test, restoring the original inflections later.
|
||||
originalInflections = Inflections.default
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
Inflections.default = originalInflections!
|
||||
}
|
||||
|
||||
private var inflectionTestCases: InflectionTestCases {
|
||||
let url = Bundle(for: type(of: self)).url(forResource: "InflectionsTests", withExtension: "json")!
|
||||
let data = try! Data(contentsOf: url)
|
||||
return try! JSONDecoder().decode(InflectionTestCases.self, from: data)
|
||||
}
|
||||
|
||||
func testPluralizePlurals() {
|
||||
XCTAssertEqual("plurals".pluralized, "plurals")
|
||||
XCTAssertEqual("Plurals".pluralized, "Plurals")
|
||||
}
|
||||
|
||||
func testPluralizeEmptyString() {
|
||||
XCTAssertEqual("".pluralized, "")
|
||||
}
|
||||
|
||||
func testUncountabilityOfASCIIWord() {
|
||||
let word = "HTTP"
|
||||
Inflections.default.uncountable(word)
|
||||
XCTAssertEqual(word.pluralized, word)
|
||||
XCTAssertEqual(word.singularized, word)
|
||||
XCTAssertEqual(word.pluralized, word.singularized)
|
||||
}
|
||||
|
||||
func testUncountabilityOfNonASCIIWord() {
|
||||
let word = "猫"
|
||||
Inflections.default.uncountable(word)
|
||||
XCTAssertEqual(word.pluralized, word)
|
||||
XCTAssertEqual(word.singularized, word)
|
||||
XCTAssertEqual(word.pluralized, word.singularized)
|
||||
}
|
||||
|
||||
func testUncountableWords() {
|
||||
for word in Inflections.default.uncountables {
|
||||
XCTAssertEqual(word.pluralized, word)
|
||||
XCTAssertEqual(word.singularized, word)
|
||||
XCTAssertEqual(word.pluralized, word.singularized)
|
||||
}
|
||||
}
|
||||
|
||||
func testUncountableCapitalizedWords() {
|
||||
for word in Inflections.default.uncountables {
|
||||
XCTAssertEqual(word.capitalized.pluralized, word.capitalized)
|
||||
XCTAssertEqual(word.capitalized.singularized, word.capitalized)
|
||||
XCTAssertEqual(word.capitalized.pluralized, word.capitalized.singularized)
|
||||
}
|
||||
}
|
||||
|
||||
func testUncountableUppercasedWords() {
|
||||
for word in Inflections.default.uncountables {
|
||||
XCTAssertEqual(word.uppercased().pluralized, word.uppercased())
|
||||
XCTAssertEqual(word.uppercased().singularized, word.uppercased())
|
||||
XCTAssertEqual(word.uppercased().pluralized, word.uppercased().singularized)
|
||||
}
|
||||
}
|
||||
|
||||
func testUncountableWordIsNotGreedy() {
|
||||
let uncountableWord = "ors"
|
||||
let countableWord = "sponsor"
|
||||
|
||||
Inflections.default.uncountable(uncountableWord)
|
||||
|
||||
XCTAssertEqual(uncountableWord.singularized, uncountableWord)
|
||||
XCTAssertEqual(uncountableWord.pluralized, uncountableWord)
|
||||
XCTAssertEqual(uncountableWord.pluralized, uncountableWord.singularized)
|
||||
|
||||
XCTAssertEqual(countableWord.singularized, "sponsor")
|
||||
XCTAssertEqual(countableWord.pluralized, "sponsors")
|
||||
XCTAssertEqual(countableWord.pluralized.singularized, "sponsor")
|
||||
}
|
||||
|
||||
func testPluralizeSingularWord() {
|
||||
for (singular, plural) in inflectionTestCases.testCases["SingularToPlural"]! {
|
||||
XCTAssertEqual(singular.pluralized, plural)
|
||||
XCTAssertEqual(singular.capitalized.pluralized, plural.capitalized)
|
||||
}
|
||||
}
|
||||
|
||||
func testSingularizePluralWord() {
|
||||
for (singular, plural) in inflectionTestCases.testCases["SingularToPlural"]! {
|
||||
XCTAssertEqual(plural.singularized, singular)
|
||||
XCTAssertEqual(plural.capitalized.singularized, singular.capitalized)
|
||||
}
|
||||
}
|
||||
|
||||
func testPluralizePluralWord() {
|
||||
for (_, plural) in inflectionTestCases.testCases["SingularToPlural"]! {
|
||||
XCTAssertEqual(plural.pluralized, plural)
|
||||
XCTAssertEqual(plural.capitalized.pluralized, plural.capitalized)
|
||||
}
|
||||
}
|
||||
|
||||
func testSingularizeSingularWord() {
|
||||
for (singular, _) in inflectionTestCases.testCases["SingularToPlural"]! {
|
||||
XCTAssertEqual(singular.singularized, singular)
|
||||
XCTAssertEqual(singular.capitalized.singularized, singular.capitalized)
|
||||
}
|
||||
}
|
||||
|
||||
func testIrregularityBetweenSingularAndPlural() {
|
||||
for (singular, plural) in inflectionTestCases.testCases["Irregularities"]! {
|
||||
Inflections.default.irregular(singular, plural)
|
||||
XCTAssertEqual(plural.singularized, singular)
|
||||
XCTAssertEqual(singular.pluralized, plural)
|
||||
XCTAssertEqual(singular.singularized, singular)
|
||||
XCTAssertEqual(plural.pluralized, plural)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct InflectionTestCases: Decodable {
|
||||
var testCases: [String: [(String, String)]]
|
||||
|
||||
struct AnyCodingKey: CodingKey {
|
||||
var stringValue: String
|
||||
var intValue: Int? { return nil }
|
||||
|
||||
init?(stringValue: String) {
|
||||
self.stringValue = stringValue
|
||||
}
|
||||
|
||||
init?(intValue: Int) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
var testCases: [String: [(String, String)]] = [:]
|
||||
let container = try decoder.container(keyedBy: AnyCodingKey.self)
|
||||
for key in container.allKeys {
|
||||
let nested = try container.nestedContainer(keyedBy: AnyCodingKey.self, forKey: key)
|
||||
for wordKey in nested.allKeys {
|
||||
let otherWord = try nested.decode(String.self, forKey: wordKey)
|
||||
testCases[key.stringValue, default: []].append((wordKey.stringValue, otherWord))
|
||||
}
|
||||
}
|
||||
self.testCases = testCases
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user