github.com/aretext/aretext@v1.3.0/syntax/languages/todotxt_test.go (about) 1 package languages 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestTodoTxtParseFunc(t *testing.T) { 10 testCases := []struct { 11 name string 12 text string 13 expected []TokenWithText 14 }{ 15 { 16 name: "description only", 17 text: "Fix a bug", 18 expected: []TokenWithText{}, 19 }, 20 { 21 name: "priority then description", 22 text: "(A) Fix a bug", 23 expected: []TokenWithText{ 24 {Text: `(A)`, Role: todoTxtPriorityRole}, 25 }, 26 }, 27 { 28 name: "completed task", 29 text: "x Fix a bug", 30 expected: []TokenWithText{ 31 {Text: `x Fix a bug`, Role: todoTxtCompletedTaskRole}, 32 }, 33 }, 34 { 35 name: "description starting with x", 36 text: "xFix", 37 expected: []TokenWithText{}, 38 }, 39 { 40 name: "single date", 41 text: "2022-04-19 Fix a bug", 42 expected: []TokenWithText{ 43 {Text: `2022-04-19`, Role: todoTxtDateRole}, 44 }, 45 }, 46 { 47 name: "multiple dates", 48 text: "2022-04-19 2022-01-02 Fix a bug", 49 expected: []TokenWithText{ 50 {Text: `2022-04-19`, Role: todoTxtDateRole}, 51 {Text: `2022-01-02`, Role: todoTxtDateRole}, 52 }, 53 }, 54 { 55 name: "project tag", 56 text: "Fix a bug +project", 57 expected: []TokenWithText{ 58 {Text: `+project`, Role: todoTxtProjectTagRole}, 59 }, 60 }, 61 { 62 name: "context tag", 63 text: "Fix a bug @context", 64 expected: []TokenWithText{ 65 {Text: `@context`, Role: todoTxtContextTagRole}, 66 }, 67 }, 68 { 69 name: "key-val tag", 70 text: "Fix a bug status:review", 71 expected: []TokenWithText{ 72 {Text: `status:`, Role: todoTxtKeyTagRole}, 73 {Text: `review`, Role: todoTxtValTagRole}, 74 }, 75 }, 76 { 77 name: "capital X at start is not completed", 78 text: "X Fix a bug", 79 expected: []TokenWithText{}, 80 }, 81 { 82 name: "full example, multiple tasks", 83 text: `(A) Thank Mom for the meatballs @phone 84 (B) Schedule Goodwill pickup +GarageSale @phone 85 Post signs around the neighborhood +GarageSale 86 @GroceryStore Eskimo pies`, 87 expected: []TokenWithText{ 88 {Text: `(A)`, Role: todoTxtPriorityRole}, 89 {Text: `@phone`, Role: todoTxtContextTagRole}, 90 {Text: `(B)`, Role: todoTxtPriorityRole}, 91 {Text: `+GarageSale`, Role: todoTxtProjectTagRole}, 92 {Text: `@phone`, Role: todoTxtContextTagRole}, 93 {Text: `+GarageSale`, Role: todoTxtProjectTagRole}, 94 {Text: `@GroceryStore`, Role: todoTxtContextTagRole}, 95 }, 96 }, 97 { 98 name: "tasks without priorities", 99 text: `Really gotta call Mom (A) @phone @someday 100 (b) Get back to the boss 101 (B)->Submit TPS report`, 102 expected: []TokenWithText{ 103 {Text: `@phone`, Role: todoTxtContextTagRole}, 104 {Text: `@someday`, Role: todoTxtContextTagRole}, 105 }, 106 }, 107 { 108 name: "task without context", 109 text: "Email SoAndSo at soandso@example.com", 110 expected: []TokenWithText{}, 111 }, 112 { 113 name: "task without project", 114 text: "Learn how to add 2+2", 115 expected: []TokenWithText{}, 116 }, 117 { 118 name: "tasks that aren't completed", 119 text: `xylophone lesson 120 X 2012-01-01 Make resolutions 121 (A) x Find ticket prices`, 122 expected: []TokenWithText{ 123 {Text: `2012-01-01`, Role: todoTxtDateRole}, 124 {Text: `(A)`, Role: todoTxtPriorityRole}, 125 }, 126 }, 127 { 128 name: "priority must be at start of line", 129 text: ` 130 (A) first 131 (B) second 132 (C) third 133 `, 134 expected: []TokenWithText{}, 135 }, 136 { 137 name: "indented with tags", 138 text: ` 139 Write the design doc +myproject @laptop 140 `, 141 expected: []TokenWithText{ 142 {Text: "+myproject", Role: todoTxtProjectTagRole}, 143 {Text: "@laptop", Role: todoTxtContextTagRole}, 144 }, 145 }, 146 } 147 148 for _, tc := range testCases { 149 t.Run(tc.name, func(t *testing.T) { 150 tokens := ParseTokensWithText(TodoTxtParseFunc(), tc.text) 151 assert.Equal(t, tc.expected, tokens) 152 }) 153 } 154 }