nu-std 0.112.1

The standard library of Nushell
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std/assert
use std/testing *
use std-rfc/str

@test
def str-dedent_simple [] {
    
    # Test 1:
    # Should start with "Heading" in the first character position
    # Should not end with a line-break
    # The blank line has no extra spaces
    assert equal (
        do {
            let s = "   
                Heading

                    one
                    two
                "
            $s | str dedent
        }
    ) $"Heading(char lsep)(char lsep)    one(char lsep)    two"
}

@test
def str-dedent_leave_blankline_whitespace [] {
    # Test 2:
    # Same as #1, but the blank line has leftover whitespace
    # indentation (16 spaces) which is left in the result
    assert equal (
        do {
            let s = "   
                Heading
                
                    one
                    two
                "
            $s | str dedent
        }
    ) $"Heading(char lsep)                (char lsep)    one(char lsep)    two"
}

@test
def str-dedent_leave_blankline_tab [] {
    # Test 3:
    # Same, but with a single tab character on the "blank" line
    assert equal (
        do {
            let s = $"   
                Heading
(char tab)
                    one
                    two
                "
            $s | str dedent
        }
    ) $"Heading(char lsep)(char tab)(char lsep)    one(char lsep)    two"
}

@test
def str-dedent_ends_with_newline [] {
    # Test 4:
    # Ends with line-break
    assert equal (
        do {
            let s = "   
                Heading

                    one
                    two

                "
            $s | str dedent
        }
    ) $"Heading(char lsep)(char lsep)    one(char lsep)    two(char lsep)"
}

@test
def str-dedent_identity [] {
    # Test 5:
    # Identity - Returns the original string sans first and last empty lines
    # No other whitespace should be removed
    assert equal (
        do {
            let s = $"(char lsep)  Identity  (char lsep)"
            $s | str dedent
        }
    ) "  Identity  "
}

@test
def str-dedent_error-no_blank_lines [] {
    # Test 6:
    # Error - Does not contain an empty first line
    assert error {||
        let s = "Error"
        $s | str dedent
    }

    # Test 6.1:
    # Error - Does not contain an empty first line
    assert error {||
        let s = $"Error(char lsep) (char lsep)Testing(char lsep)"
        $s | str dedent
    }
}

@test
def str-dedent_error-no_blank_first_line [] {
    # Test 7:
    # Error - Does not contain an empty last line
    assert error {||
        let s = "
            Error"
        $s | str dedent
    }
}

@test
def str-dedent_error-missing_last_empty_line [] {
    # Test 7.1:
    # Error - Does not contain an empty last line
    assert error {||
        let s = "

            Error"
        $s | str dedent
    }
}

@test
def str-dedent_error-not_enough_indentation [] {
    # Test 8:
    # Error - Line 1 does not have enough indentation
    assert error {||
        let s = "   
           Line 1
            Line 2
            "
        $s | str dedent
    }
}

@test
def str-dedent_error-not_enough_indentation2 [] {
    # Test 8.1:
    # Error - Line 2 does not have enough indentation
    assert error {||
        let s = "   
            Line 1
           Line 2
            "
        $s | str dedent
    }
}

@test
def str-dedent_error-not_enough_indentation3 [] {
    # Test 8.2:
    # Error - Line does not have enough indentation
    assert error {||
        let s = "   
           Line  
            "
        $s | str dedent
    }
}

@test
def str-dedent_first_line_whitespace_allowed [] {
    # Test 9:
    # "Hidden" whitespace on the first line is allowed
    assert equal (
        do {
            let s = $"   (char tab) (char lsep)  Identity  (char lsep)"
            $s | str dedent
        }
    ) "  Identity  "
}

@test
def str-dedent_using_tabs [] {
    # Test 10:
    # If the indentation on the last line uses tabs, then the number of tabs
    # will be used instead of spaces
    let actual = (
        $"(char lsep)(char tab)(char tab)First line(char lsep)(char tab)(char tab)(char tab)Second line(char lsep)(char tab)(char tab)"
        | str dedent
    )

    let expected = $"First line(char lsep)(char tab)Second line"

    assert equal $actual $expected
}

@test
def str-unindent_simple [] {
    # Test 1:
    # Should start with "Heading" in the first character position
    # Should not end with a line-break
    # The blank line has no extra spaces
    let actual = (
        "   
            Heading

                one
                two
        "
        | str unindent
    )

    let expected = $"Heading(char lsep)(char lsep)    one(char lsep)    two"

    assert equal $actual $expected
}

@test
def str-unindent_ignore_first_and_last_whitespace [] {
    # Test 2:
    # If the first and/or last line are only whitespace
    # then they shouldn't be included in the result

    let actual = "   
            Heading

                one
                two
        "
        | str unindent

    let expected = "            Heading

                one
                two"
        | str unindent

    assert equal $actual $expected
}

@test
def str-unindent_keep_extra_line [] {
  # Test 3:
  # Keep intentional blank lines at start and/or end

  let actual = "

  Content

  " | str unindent

  let expected = $"(char lsep)Content(char lsep)"

  assert equal $actual $expected
}

@test
def str-unindent_works_on_single_line [] {
    # Test 4:
    # Works on a single-line string
    # And trailing whitespace is preserved

    let actual = ("    Content  " | str unindent)
    let expected = "Content  "

    assert equal $actual $expected
}

@test
def str-unindent_whitespace_only_single_line [] {
    # Test 4:
    # Works on a single-line string with whitespace-only
    # Returns the original string

    let actual = ("   " | str unindent)
    let expected = "   "

    assert equal $actual $expected
}
    
@test
def str-unindent_whitespace_works_with_tabs [] {
    # Test 4:
    # Works with tabs for indentation

    let actual = (
        $"(char lsep)(char tab)(char tab)Content(char lsep)"
        | str unindent --tabs
    )

    let expected = "Content"

    assert equal $actual $expected
}

@test
def str-align_simple [] {
    let actual = [
        "let a = 1"
        "let max = 2"
        "let very_long_variable_name = 3"
    ] | str align '='

    let expected = [
        "let a                       = 1"
        "let max                     = 2"
        "let very_long_variable_name = 3"
    ] | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_center [] {
    let actual = [
        "a = 1"
        "max = 2"
        "very_long_variable_name = 3"
    ] | str align '=' --center

    let expected = [
        "                      a = 1"
        "                    max = 2"
        "very_long_variable_name = 3"
    ] | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_with_range [] {
    let actual = r#'match 5 {
    1.. => { print "More than zero" }
    0 => { print "Zero" }
    -1 => { print "Negative one" }
    -119283 => { print "Very negative" }
}'# | str align '=>' --range 2..

    let expected = r#'match 5 {
    1.. => { print "More than zero" }
    0       => { print "Zero" }
    -1      => { print "Negative one" }
    -119283 => { print "Very negative" }
}'# | lines | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_ignore_lines_with_no_target [] {
    let actual = [
        "let a = 1"
        "let max = 2"
        "# comment"
    ] | str align '='

    let expected = [
        "let a   = 1"
        "let max = 2"
        "# comment"
    ] | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_use_different_char [] {
    let actual = [
        "=>"
        "=====>"
    ] | str align '>' -c '='

    let expected = [
        "=====>"
        "=====>"
    ] | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_multiple_target_in_line [] {
    let actual = [
        "print test # Hello # World"
        "print hello there # test"
    ] | str align '#'

    let expected = [
        "print test        # Hello # World"
        "print hello there # test"
    ] | str join "\n"

    assert equal $actual $expected
}

@test
def str-align_no_target [] {

    let expected = [
        "print test # Hello # World"
        "print hello there # test"
    ] | str join "\n"

    let actual = $expected | str align '='

    assert equal $actual $expected
}

@test
def str-align_empty_target_noop [] {

    let expected = [
        "print test # Hello # World"
        "print hello there # test"
    ] | str join "\n"

    let actual = $expected | str align ''

    assert equal $actual $expected
}

@test
def str-align_empty_input_noop [] {

    let expected = ""

    let actual = [] | str align '='

    assert equal $actual $expected
}