summary refs log tree commit diff stats
path: root/raku/luhn/luhn.rakutest
blob: 4bd0e639c0c4d696420f4c5ccf2b7434224050c0 (plain) (blame)
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
#!/usr/bin/env raku
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Luhn;
plan 18;

my @test-cases = from-json($=pod.pop.contents).List;
for @test-cases -> %case {
  subtest %case<description>, {
    plan 2;
    isa-ok ( my $result := is-luhn-valid %case<input><value> ), Bool;
    is-deeply $result, %case<expected>, 'Result matches expected';
  }
}

=head2 Test Cases
=begin code
[
  {
    "description": "single digit strings can not be valid",
    "expected": false,
    "input": {
      "value": "1"
    },
    "property": "valid"
  },
  {
    "description": "a single zero is invalid",
    "expected": false,
    "input": {
      "value": "0"
    },
    "property": "valid"
  },
  {
    "description": "a simple valid SIN that remains valid if reversed",
    "expected": true,
    "input": {
      "value": "059"
    },
    "property": "valid"
  },
  {
    "description": "a simple valid SIN that becomes invalid if reversed",
    "expected": true,
    "input": {
      "value": "59"
    },
    "property": "valid"
  },
  {
    "description": "a valid Canadian SIN",
    "expected": true,
    "input": {
      "value": "055 444 285"
    },
    "property": "valid"
  },
  {
    "description": "invalid Canadian SIN",
    "expected": false,
    "input": {
      "value": "055 444 286"
    },
    "property": "valid"
  },
  {
    "description": "invalid credit card",
    "expected": false,
    "input": {
      "value": "8273 1232 7352 0569"
    },
    "property": "valid"
  },
  {
    "description": "invalid long number with an even remainder",
    "expected": false,
    "input": {
      "value": "1 2345 6789 1234 5678 9012"
    },
    "property": "valid"
  },
  {
    "description": "valid number with an even number of digits",
    "expected": true,
    "input": {
      "value": "095 245 88"
    },
    "property": "valid"
  },
  {
    "description": "valid number with an odd number of spaces",
    "expected": true,
    "input": {
      "value": "234 567 891 234"
    },
    "property": "valid"
  },
  {
    "description": "valid strings with a non-digit added at the end become invalid",
    "expected": false,
    "input": {
      "value": "059a"
    },
    "property": "valid"
  },
  {
    "description": "valid strings with punctuation included become invalid",
    "expected": false,
    "input": {
      "value": "055-444-285"
    },
    "property": "valid"
  },
  {
    "description": "valid strings with symbols included become invalid",
    "expected": false,
    "input": {
      "value": "055# 444$ 285"
    },
    "property": "valid"
  },
  {
    "description": "single zero with space is invalid",
    "expected": false,
    "input": {
      "value": " 0"
    },
    "property": "valid"
  },
  {
    "description": "more than a single zero is valid",
    "expected": true,
    "input": {
      "value": "0000 0"
    },
    "property": "valid"
  },
  {
    "description": "input digit 9 is correctly converted to output digit 9",
    "expected": true,
    "input": {
      "value": "091"
    },
    "property": "valid"
  },
  {
    "description": "using ascii value for non-doubled non-digit isn't allowed",
    "expected": false,
    "input": {
      "value": "055b 444 285"
    },
    "property": "valid"
  },
  {
    "description": "using ascii value for doubled non-digit isn't allowed",
    "expected": false,
    "input": {
      "value": ":9"
    },
    "property": "valid"
  }
]
=end code