github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/dsref/generate_test.go (about) 1 package dsref 2 3 import ( 4 "testing" 5 ) 6 7 const TestPrefix = "prefix_" 8 9 func TestGenerateName(t *testing.T) { 10 cases := []struct { 11 description string 12 input string 13 expect string 14 }{ 15 { 16 "simple string", 17 "data", 18 "data", 19 }, 20 { 21 "title case", 22 "Information", 23 "information", 24 }, 25 { 26 "underscores stay the same", 27 "body_input", 28 "body_input", 29 }, 30 { 31 "spaces replaced", 32 "sample population 2019", 33 "sample_population_2019", 34 }, 35 { 36 "spaces are trimmed at start and end", 37 " good name ", 38 "good_name", 39 }, 40 { 41 "control characters are ignored", 42 "some\tweird\rchar\ncodes\x07in\x7fname", 43 "someweirdcharcodesinname", 44 }, 45 { 46 "punctuation followed by space makes one dash", 47 "category: annual", 48 "category-annual", 49 }, 50 { 51 "split words are separated by underscore", 52 "final1997report", 53 "final_1997_report", 54 }, 55 { 56 "many words separated by punctuation that will be cut", 57 "en.wikipedia.org,List_of_highest-grossing_films,Highest-grossing_films", 58 "en-wikipedia-org-list_of_highest-grossing", 59 }, 60 { 61 "lots of words that will cut off at word boundary", 62 "Title, artist, date, and medium of every artwork in the MoMA collection", 63 "title-artist-date-and_medium_of_every", 64 }, 65 { 66 "single long word that has to be truncated", 67 "Pneumonoultramicroscopicsilicovolcanoconiosis", 68 "pneumonoultramicroscopicsilicovolcanoconiosi", 69 }, 70 { 71 "single long word in Icelandic uses non-ascii characters", 72 "Vaðlaheiðarvegavinnuverkfærageymsluskúrslyklakippuhringurinn", 73 "valaheiarvegavinnuverkfrageymsluskurslyklaki", 74 }, 75 { 76 "start with number and contains spaces", 77 "2018 winners", 78 "prefix_2018_winners", 79 }, 80 { 81 "dashes stay", 82 "2015-09-16..2016-09-30", 83 "prefix_2015-09-16--2016-09-30", 84 }, 85 { 86 "unicode normalize to ascii", 87 "pira\u00f1a_data", 88 "pirana_data", 89 }, 90 { 91 "camel case with number", 92 "EconIndicatorNominalGDP1997China", 93 "econ_indicator_nominal_gdp_1997_china", 94 }, 95 { 96 "camel case with all caps word", 97 "MonthlyHTTPTraffic", 98 "monthly_http_traffic", 99 }, 100 } 101 for _, c := range cases { 102 t.Run(c.description, func(t *testing.T) { 103 actual := GenerateName(c.input, TestPrefix) 104 if actual != c.expect { 105 t.Errorf("mismatch, expect: %s, got: %s", c.expect, actual) 106 } 107 }) 108 } 109 } 110 111 // Test the edge case of cutting off at a word boundary. The right-side always has a whole word, 112 // and does not ever have a space. 113 func TestGenerateNameCutoffWord(t *testing.T) { 114 realLen := NameMaxLength 115 defer func() { 116 NameMaxLength = realLen 117 }() 118 NameMaxLength = 18 119 120 cases := []struct { 121 input string 122 expect string 123 }{ 124 { 125 "the quick brown fox jumped", 126 "the_quick_brown", 127 }, 128 { 129 "the quic brown fox jumped", 130 "the_quic_brown_fox", 131 }, 132 { 133 "the qui brown fox jumped", 134 "the_qui_brown_fox", 135 }, 136 { 137 "the qu brown fox jumped", 138 "the_qu_brown_fox", 139 }, 140 { 141 "the q brown fox jumped", 142 "the_q_brown_fox", 143 }, 144 { 145 "the brown fox jumped", 146 "the__brown_fox", 147 }, 148 { 149 "the brown fox jumped", 150 "the_brown_fox", 151 }, 152 { 153 "the brow fox jumped", 154 "the_brow_fox", 155 }, 156 { 157 "the bro fox jumped", 158 "the_bro_fox_jumped", 159 }, 160 } 161 for i, c := range cases { 162 actual := GenerateName(c.input, TestPrefix) 163 if actual != c.expect { 164 t.Errorf("case %d: mismatch, expect: %s, got: %s", i, c.expect, actual) 165 } 166 } 167 }