1package app23import (4 "sort"5 "testing"6)78// The two orders, held to the browser that recorded the baselines.9//10// Both expectations below were produced by asking a browser the same question11// the reference implementation asked it —12//13// titles.sort((a, b) => a.localeCompare(b))14// titles.sort((a, b) => a.localeCompare(b, undefined, {numeric: true, sensitivity: "base"}))15//16// — over titles taken from the six fixture volumes, plus a handful of pairs17// the fixture set does not happen to contain (a case pair, a numeric ladder)18// so the two orders can be told apart here, over this very corpus. The19// input order is part of the fixture: both sorts are stable, and where the20// collation calls two titles equal it is the input order that decides.21var collationInput = []string{22 "¡La Fantoma!",23 "¡La Fantoma! (x4)",24 "??? Fire Sword",25 ".357 Magnum Revolver",26 `"Droplet"`,27 "020 - Solitary Moving Stable",28 "101 North",29 "15th Street Trail Connector",30 "9mm Pistol",31 "A.C. Lucas House",32 "Batty’s Hotel",33 "Batty's Hotel",34 "Cádiz",35 "Cañas",36 "Handlová",37 "John's Landmark",38 "Kerry Eurodyne’s Residence",39 "O'Donnel Building",40 "Octavio’s Clinic",41 "Z-43 Innovative Toxins Plant",42 "Zetatech Sandevistan Mk.1",43 "Zetatech Sandevistan Mk.3",44 "apple",45 "Apple",46 "Banana",47 "banana",48 "Level 2",49 "Level 10",50 "Level 1",51}5253func TestCompareTitlesIsTheBrowsersOrder(t *testing.T) {54 want := []string{55 "¡La Fantoma!",56 "¡La Fantoma! (x4)",57 "??? Fire Sword",58 ".357 Magnum Revolver",59 `"Droplet"`,60 "020 - Solitary Moving Stable",61 "101 North",62 "15th Street Trail Connector",63 "9mm Pistol",64 "A.C. Lucas House",65 "apple",66 "Apple",67 "banana",68 "Banana",69 "Batty's Hotel",70 "Batty’s Hotel",71 "Cádiz",72 "Cañas",73 "Handlová",74 "John's Landmark",75 "Kerry Eurodyne’s Residence",76 "Level 1",77 "Level 10",78 "Level 2",79 "O'Donnel Building",80 "Octavio’s Clinic",81 "Z-43 Innovative Toxins Plant",82 "Zetatech Sandevistan Mk.1",83 "Zetatech Sandevistan Mk.3",84 }85 got := append([]string(nil), collationInput...)86 sort.SliceStable(got, func(i, j int) bool { return compareTitles(got[i], got[j]) < 0 })87 assertOrder(t, want, got)88}8990func TestCompareIndexTitlesIsNumericAndCaseBlind(t *testing.T) {91 want := []string{92 "¡La Fantoma!",93 "¡La Fantoma! (x4)",94 "??? Fire Sword",95 ".357 Magnum Revolver",96 `"Droplet"`,97 "9mm Pistol",98 "15th Street Trail Connector",99 "020 - Solitary Moving Stable",100 "101 North",101 "A.C. Lucas House",102 "apple",103 "Apple",104 "Banana",105 "banana",106 "Batty’s Hotel",107 "Batty's Hotel",108 "Cádiz",109 "Cañas",110 "Handlová",111 "John's Landmark",112 "Kerry Eurodyne’s Residence",113 "Level 1",114 "Level 2",115 "Level 10",116 "O'Donnel Building",117 "Octavio’s Clinic",118 "Z-43 Innovative Toxins Plant",119 "Zetatech Sandevistan Mk.1",120 "Zetatech Sandevistan Mk.3",121 }122 got := append([]string(nil), collationInput...)123 sort.SliceStable(got, func(i, j int) bool { return compareIndexTitles(got[i], got[j]) < 0 })124 assertOrder(t, want, got)125}126127// A byte comparison is what this replaced, and it is worth one line saying so:128// the title the browser puts first is the one `strings.ToLower` puts last.129func TestCollationIsNotAByteOrder(t *testing.T) {130 if compareTitles("¡La Fantoma!", "188 Trading Post") >= 0 {131 t.Fatal("punctuation must sort before digits, as the reference ordered it")132 }133 if compareIndexTitles("Akkala Highlands", "akkala highlands") != 0 {134 t.Fatal("the feature index ignores case, as {sensitivity: base} asked it to")135 }136}137138func assertOrder(t *testing.T, want, got []string) {139 t.Helper()140 if len(want) != len(got) {141 t.Fatalf("sorted %d titles, expected %d", len(got), len(want))142 }143 for at := range want {144 if got[at] != want[at] {145 t.Fatalf("position %d is %q, the browser put %q there\n got: %q\nwant: %q",146 at, got[at], want[at], got, want)147 }148 }149}