| 1 | package fichas |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func fd(key, kind string, tier Tier) FieldDef { |
| 6 | return FieldDef{Key: key, Kind: kind, Tier: tier} |
| 7 | } |
| 8 | |
| 9 | // The guard seal.Print could not enforce. |
| 10 | func TestPrintForRefusesAFieldThatIsNotPrinted(t *testing.T) { |
| 11 | s := testSealer(t) |
| 12 | for _, tier := range []Tier{TierSealed, TierPlain, Tier("")} { |
| 13 | if _, err := printFor(s, fd(FieldStage, KindEnum, tier), "Lead"); err == nil { |
| 14 | t.Errorf("a %q field was printed; its equality classes would be published", tier) |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func TestPrintForAcceptsAPrintedField(t *testing.T) { |
| 20 | s := testSealer(t) |
| 21 | got, err := printFor(s, fd(FieldEmail, KindText, TierPrint), "ana@example.com") |
| 22 | if err != nil { |
| 23 | t.Fatalf("printFor: %v", err) |
| 24 | } |
| 25 | if got == "" { |
| 26 | t.Fatal("printFor returned an empty print with no error") |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Without this the dedupe the print exists for misses on the commonest |
| 31 | // real variance, and "do we already have this contact" quietly stops |
| 32 | // working. |
| 33 | func TestPrintForFoldsTheVarianceThatIsNotIdentity(t *testing.T) { |
| 34 | s := testSealer(t) |
| 35 | email := fd(FieldEmail, KindText, TierPrint) |
| 36 | phone := fd(FieldPhone, KindText, TierPrint) |
| 37 | |
| 38 | same := func(f FieldDef, a, b string) { |
| 39 | t.Helper() |
| 40 | pa, err := printFor(s, f, a) |
| 41 | if err != nil { |
| 42 | t.Fatalf("printFor(%q): %v", a, err) |
| 43 | } |
| 44 | pb, err := printFor(s, f, b) |
| 45 | if err != nil { |
| 46 | t.Fatalf("printFor(%q): %v", b, err) |
| 47 | } |
| 48 | if pa != pb { |
| 49 | t.Errorf("%q and %q print differently under %s", a, b, f.Key) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | same(email, "ana@example.com", "Ana@Example.com") |
| 54 | same(email, "ana@example.com", " ana@example.com ") |
| 55 | same(phone, "+353 1 555 0100", "+35315550100") |
| 56 | same(phone, "(01) 555-0100", "015550100") |
| 57 | } |
| 58 | |
| 59 | // Canonicalisation must not merge things that are genuinely different. |
| 60 | func TestPrintForKeepsDifferentValuesDifferent(t *testing.T) { |
| 61 | s := testSealer(t) |
| 62 | name := fd(FieldName, KindText, TierPrint) |
| 63 | a, _ := printFor(s, name, "Cnoc Teoranta") |
| 64 | b, _ := printFor(s, name, "Cnoc Limited") |
| 65 | if a == b { |
| 66 | t.Fatal("two different names print the same") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // A name with digits in it must not be treated as a phone number and have |
| 71 | // its letters stripped. |
| 72 | func TestCanonicalDoesNotMangleANameWithDigits(t *testing.T) { |
| 73 | if got := canonical(KindText, "Studio 54 Ltd"); got != "studio 54 ltd" { |
| 74 | t.Fatalf("canonical mangled a name: %q", got) |
| 75 | } |
| 76 | } |
| 77 | |