1 package rulesfn
2
3 import (
4 "testing"
5 "github.com/aws/smithy-go/ptr"
6 )
7
8 func TestSubString(t *testing.T) {
9 cases := map[string]struct {
10 input string
11 start, stop int
12 reverse bool
13 expect *string
14 }{
15 "prefix": {
16 input: "abcde", start: 0, stop: 3, reverse: false,
17 expect: ptr.String("abc"),
18 },
19 "prefix max-ascii": {
20 input: "abcde\u007F", start: 0, stop: 3, reverse: false,
21 expect: ptr.String("abc"),
22 },
23 "suffix reverse": {
24 input: "abcde", start: 0, stop: 3, reverse: true,
25 expect: ptr.String("cde"),
26 },
27 "too long": {
28 input: "ab", start: 0, stop: 3, reverse: false,
29 expect: nil,
30 },
31 "invalid start index": {
32 input: "ab", start: -1, stop: 3, reverse: false,
33 expect: nil,
34 },
35 "invalid stop index": {
36 input: "ab", start: 0, stop: 0, reverse: false,
37 expect: nil,
38 },
39 "non-ascii": {
40 input: "ab🐱", start: 0, stop: 1, reverse: false,
41 expect: nil,
42 },
43 }
44
45 for name, c := range cases {
46 t.Run(name, func(t *testing.T) {
47 actual := SubString(c.input, c.start, c.stop, c.reverse)
48 if c.expect == nil {
49 if actual != nil {
50 t.Fatalf("expect no result, got %v", *actual)
51 }
52 return
53 }
54
55 if actual == nil {
56 t.Fatalf("expect result, got none")
57 }
58
59 if e, a := *c.expect, *actual; e != a {
60 t.Errorf("expect %q, got %q", e, a)
61 }
62 })
63 }
64 }
65
View as plain text