cpf_cnpj 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ module CpfCnpj
2
+ class Generator
3
+ NUMBERS = [*0..9].freeze
4
+
5
+ def self.generate(size, verifier_digit_generator)
6
+ numbers = size.times.map { NUMBERS.sample }
7
+ numbers << verifier_digit_generator.generate(numbers)
8
+ numbers << verifier_digit_generator.generate(numbers)
9
+ numbers.join("")
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,11 @@
1
+ module CpfCnpj
2
+ VERSION = "0.3.0"
3
+ end
4
+
1
5
  class CPF
2
- VERSION = "0.2.1"
6
+ VERSION = CpfCnpj::VERSION
3
7
  end
4
8
 
5
9
  class CNPJ
6
- VERSION = "0.2.1"
10
+ VERSION = CpfCnpj::VERSION
7
11
  end
@@ -0,0 +1,14 @@
1
+ module CaptureSyscall
2
+ def capture_syscall
3
+ exit_status = nil
4
+
5
+ stdout, stderr = Timeout.timeout(1) do
6
+ capture_subprocess_io do
7
+ yield
8
+ exit_status = $CHILD_STATUS.exitstatus
9
+ end
10
+ end
11
+
12
+ [exit_status, stdout, stderr]
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+ Bundler.require
4
+
5
+ require "minitest/utils"
6
+ require "minitest/autorun"
7
+
8
+ require_relative "./support/capture_syscall"
@@ -0,0 +1,132 @@
1
+ require "test_helper"
2
+
3
+ module CNPJCli
4
+ class CheckTest < Minitest::Test
5
+ include CaptureSyscall
6
+
7
+ %w[-c --check].each do |switch|
8
+ test "checks if provided number is valid [using #{switch}]" do
9
+ exit_status, stdout = capture_syscall do
10
+ system "./bin/cnpj #{switch} 54550752000155"
11
+ end
12
+
13
+ assert_equal 0, exit_status
14
+ assert_equal stdout, ""
15
+ end
16
+
17
+ test "outputs error message if provided number is invalid [using #{switch}]" do
18
+ exit_status, _, stderr = capture_syscall do
19
+ system "./bin/cnpj #{switch} invalid"
20
+ end
21
+
22
+ assert_equal 1, exit_status
23
+ assert stderr.include?("Error: Invalid number")
24
+ end
25
+ end
26
+ end
27
+
28
+ class HelpTest < Minitest::Test
29
+ include CaptureSyscall
30
+
31
+ %w[-h --help].each do |switch|
32
+ test "outputs help [using #{switch}]" do
33
+ exit_status, _, stderr = capture_syscall do
34
+ system "./bin/cnpj #{switch}"
35
+ end
36
+
37
+ assert_equal 1, exit_status
38
+ assert stderr.include?("Usage: cnpj")
39
+ end
40
+ end
41
+
42
+ test "outputs help on tail" do
43
+ exit_status, _, stderr = capture_syscall do
44
+ system "./bin/cnpj"
45
+ end
46
+
47
+ assert_equal 1, exit_status
48
+ assert stderr.include?("Usage: cnpj")
49
+ end
50
+ end
51
+
52
+ class VersionTest < Minitest::Test
53
+ include CaptureSyscall
54
+
55
+ %w[-v --version].each do |switch|
56
+ test "outputs version [using #{switch}]" do
57
+ exit_status, stdout = capture_syscall do
58
+ system "./bin/cnpj #{switch}"
59
+ end
60
+
61
+ assert_equal 0, exit_status
62
+ assert stdout.include?(CNPJ::VERSION.to_s)
63
+ end
64
+ end
65
+ end
66
+
67
+ class GenerateTest < Minitest::Test
68
+ include CaptureSyscall
69
+
70
+ %w[-g --generate].each do |switch|
71
+ test "generates number [using #{switch}]" do
72
+ exit_status, stdout = capture_syscall do
73
+ system "./bin/cnpj #{switch}"
74
+ end
75
+
76
+ assert_equal 0, exit_status
77
+ assert_match CNPJ::REGEX, stdout
78
+ end
79
+ end
80
+
81
+ test "generates stripped number" do
82
+ exit_status, stdout = capture_syscall do
83
+ system "./bin/cnpj -gs"
84
+ end
85
+
86
+ assert_equal 0, exit_status
87
+ assert_match /\A\d{14}\Z/, stdout
88
+ end
89
+ end
90
+
91
+ class FormatTest < Minitest::Test
92
+ include CaptureSyscall
93
+
94
+ %w[-f --format].each do |switch|
95
+ test "formats argument [using #{switch}]" do
96
+ exit_status, stdout = capture_syscall do
97
+ system "./bin/cnpj #{switch} 54550752000155"
98
+ end
99
+
100
+ assert_equal 0, exit_status
101
+ assert stdout.include?("54.550.752/0001-55")
102
+ end
103
+ end
104
+
105
+ test "formats argument using stdin" do
106
+ exit_status, stdout = capture_syscall do
107
+ system "echo 54550752000155 | ./bin/cnpj --format"
108
+ end
109
+
110
+ assert_equal 0, exit_status
111
+ assert stdout.include?("54.550.752/0001-55")
112
+ end
113
+
114
+ test "fails when providing invalid number" do
115
+ exit_status, _, stderr = capture_syscall do
116
+ system "./bin/cnpj --format invalid"
117
+ end
118
+
119
+ assert_equal 1, exit_status
120
+ assert stderr.include?("Error: Invalid number")
121
+ end
122
+
123
+ test "fails when not providing a number" do
124
+ exit_status, _, stderr = capture_syscall do
125
+ system "./bin/cnpj --format"
126
+ end
127
+
128
+ assert_equal 1, exit_status
129
+ assert stderr.include?("Error: Invalid number")
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ class CNPJFormatterTest < Minitest::Test
4
+ test "formats strings without separators" do
5
+ number = "12345678901234"
6
+ assert_equal "12.345.678/9012-34", CNPJ::Formatter.format(number)
7
+ end
8
+
9
+ test "removes any non-numeric characters" do
10
+ number = "\n12$345[678/9012-34"
11
+ assert_equal "12345678901234", CNPJ::Formatter.strip(number)
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require "test_helper"
2
+
3
+ class CNPJGeneratorTest < Minitest::Test
4
+ test "generates valid numbers" do
5
+ 10.times { assert CNPJ.valid?(CNPJ.generate) }
6
+ end
7
+
8
+ test "generates random numbers" do
9
+ 10.times { refute_equal CNPJ.generate, CNPJ.generate }
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require "test_helper"
2
+
3
+ class CnpjTest < Minitest::Test
4
+ test "blacklists common numbers" do
5
+ refute CNPJ.valid?("11111111111111")
6
+ refute CNPJ.valid?("22222222222222")
7
+ refute CNPJ.valid?("33333333333333")
8
+ refute CNPJ.valid?("44444444444444")
9
+ refute CNPJ.valid?("55555555555555")
10
+ refute CNPJ.valid?("66666666666666")
11
+ refute CNPJ.valid?("77777777777777")
12
+ refute CNPJ.valid?("88888888888888")
13
+ refute CNPJ.valid?("99999999999999")
14
+ end
15
+
16
+ test "rejects blank strings" do
17
+ refute CNPJ.valid?("")
18
+ end
19
+
20
+ test "rejects nil values" do
21
+ refute CNPJ.valid?(nil)
22
+ end
23
+
24
+ test "validates formatted strings" do
25
+ number = "54.550.752/0001-55"
26
+ assert CNPJ.valid?(number)
27
+ end
28
+
29
+ test "validates unformatted strings" do
30
+ number = "54550752000155"
31
+ assert CNPJ.valid?(number)
32
+ end
33
+
34
+ test "validates messed strings" do
35
+ number = "54550[752#0001..$55"
36
+ assert CNPJ.valid?(number)
37
+ end
38
+
39
+ test "generates formatted number" do
40
+ assert_match %r[\A\d{2}\.\d{3}\.\d{3}/\d{4}-\d{2}\z], CNPJ.generate(true)
41
+ end
42
+
43
+ test "generates stripped number" do
44
+ assert_match /\A\d{14}\z/, CNPJ.generate
45
+ end
46
+ end
@@ -0,0 +1,134 @@
1
+ require "test_helper"
2
+
3
+ module CPFCli
4
+ class CheckTest < Minitest::Test
5
+ include CaptureSyscall
6
+
7
+ %w[-c --check].each do |switch|
8
+ test "checks if provided number is valid [using #{switch}]" do
9
+ exit_status, stdout = capture_syscall do
10
+ system "./bin/cpf #{switch} 76616598837"
11
+ end
12
+
13
+ assert_equal 0, exit_status
14
+ assert_equal stdout, ""
15
+ end
16
+
17
+ test "outputs error message if provided number is invalid [using #{switch}]" do
18
+ exit_status, _, stderr = capture_syscall do
19
+ system "./bin/cpf #{switch} invalid"
20
+ end
21
+
22
+ assert_equal 1, exit_status
23
+ assert stderr.include?("Error: Invalid number")
24
+ end
25
+ end
26
+ end
27
+
28
+ class HelpTest < Minitest::Test
29
+ include CaptureSyscall
30
+
31
+ %w[-h --help].each do |switch|
32
+ test "outputs help [using #{switch}]" do
33
+ exit_status, _, stderr = capture_syscall do
34
+ system "./bin/cpf #{switch}"
35
+ end
36
+
37
+ assert_equal 1, exit_status
38
+ assert stderr.include?("Usage: cpf")
39
+ end
40
+ end
41
+
42
+ test "outputs help on tail" do
43
+ exit_status, _, stderr = capture_syscall do
44
+ system "./bin/cpf"
45
+ end
46
+
47
+ assert_equal 1, exit_status
48
+ assert stderr.include?("Usage: cpf")
49
+ end
50
+ end
51
+
52
+ class VersionTest < Minitest::Test
53
+ include CaptureSyscall
54
+
55
+ %w[-v --version].each do |switch|
56
+ test "outputs version [using #{switch}]" do
57
+ exit_status, stdout, stderr = capture_syscall do
58
+ system "./bin/cpf #{switch}"
59
+ end
60
+
61
+ assert_equal 0, exit_status
62
+ assert stdout.include?(CPF::VERSION.to_s)
63
+ end
64
+ end
65
+ end
66
+
67
+ class GenerateTest < Minitest::Test
68
+ include CaptureSyscall
69
+
70
+ %w[-g --generate].each do |switch|
71
+ test "generates number [using #{switch}]" do
72
+ exit_status, stdout = capture_syscall do
73
+ system "./bin/cpf #{switch}"
74
+ end
75
+
76
+ assert_equal 0, exit_status
77
+ assert_match CPF::REGEX, stdout
78
+ end
79
+ end
80
+
81
+ test "generates stripped number" do
82
+ exit_status, stdout = capture_syscall do
83
+ system "./bin/cpf -gs"
84
+ end
85
+
86
+ assert_equal 0, exit_status
87
+ assert_match /\A\d{11}\Z/, stdout
88
+ end
89
+ end
90
+
91
+ class FormatTest < Minitest::Test
92
+ include CaptureSyscall
93
+
94
+ %w[-f --format].each do |switch|
95
+ test "formats argument [using #{switch}]" do
96
+ exit_status, stdout = capture_syscall do
97
+ system "./bin/cpf #{switch} 76616598837"
98
+ end
99
+
100
+ assert_equal 0, exit_status
101
+ assert stdout.include?("766.165.988-37")
102
+ end
103
+ end
104
+
105
+ test "formats argument using stdin" do
106
+ stdin = StringIO.new("76616598837")
107
+
108
+ exit_status, stdout = capture_syscall do
109
+ system "echo 76616598837 | ./bin/cpf --format"
110
+ end
111
+
112
+ assert_equal 0, exit_status
113
+ assert stdout.include?("766.165.988-37")
114
+ end
115
+
116
+ test "fails when providing invalid number" do
117
+ exit_status, _, stderr = capture_syscall do
118
+ system "./bin/cpf --format invalid"
119
+ end
120
+
121
+ assert_equal 1, exit_status
122
+ assert stderr.include?("Error: Invalid number")
123
+ end
124
+
125
+ test "fails when not providing a number" do
126
+ exit_status, _, stderr = capture_syscall do
127
+ system "./bin/cpf --format"
128
+ end
129
+
130
+ assert_equal 1, exit_status
131
+ assert stderr.include?("Error: Invalid number")
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ class CPFFormatterTest < Minitest::Test
4
+ test "formats strings without separators" do
5
+ number = "12345678901234"
6
+ assert_equal "12.345.678/9012-34", CNPJ::Formatter.format(number)
7
+ end
8
+
9
+ test "removes any non-numeric characters" do
10
+ number = "\n12$345[678/9012-34"
11
+ assert_equal "12345678901234", CNPJ::Formatter.strip(number)
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require "test_helper"
2
+
3
+ class CPFGeneratorTest < Minitest::Test
4
+ test "generates valid numbers" do
5
+ 10.times { assert CNPJ.valid?(CNPJ.generate) }
6
+ end
7
+
8
+ test "generates random numbers" do
9
+ 10.times { refute_equal CNPJ.generate, CNPJ.generate }
10
+ end
11
+ end
@@ -0,0 +1,71 @@
1
+ require "test_helper"
2
+
3
+ class CpfTest < Minitest::Test
4
+ test "blacklists common numbers" do
5
+ refute CPF.valid?("11111111111")
6
+ refute CPF.valid?("22222222222")
7
+ refute CPF.valid?("33333333333")
8
+ refute CPF.valid?("44444444444")
9
+ refute CPF.valid?("55555555555")
10
+ refute CPF.valid?("66666666666")
11
+ refute CPF.valid?("77777777777")
12
+ refute CPF.valid?("88888888888")
13
+ refute CPF.valid?("99999999999")
14
+ refute CPF.valid?("12345678909")
15
+ end
16
+
17
+ test "rejects blank strings" do
18
+ refute CPF.valid?("")
19
+ end
20
+
21
+ test "rejects nil values" do
22
+ refute CPF.valid?(nil)
23
+ end
24
+
25
+ test "validates formatted strings" do
26
+ number = "295.379.955-93"
27
+ assert CPF.valid?(number)
28
+ end
29
+
30
+ test "validates unformatted strings" do
31
+ number = "29537995593"
32
+ assert CPF.valid?(number)
33
+ end
34
+
35
+ test "validates messed strings" do
36
+ number = "295$379\n955...93"
37
+ assert CPF.valid?(number)
38
+ end
39
+
40
+ test "strictly validates strings" do
41
+ refute CPF.valid?("295$379\n955...93", strict: true)
42
+ assert CPF.valid?("295.379.955-93", strict: true)
43
+ assert CPF.valid?("29537995593", strict: true)
44
+ end
45
+
46
+ test "returns stripped number" do
47
+ cpf = CPF.new("295.379.955-93")
48
+ assert_equal "29537995593", cpf.stripped
49
+ end
50
+
51
+ test "formats number" do
52
+ cpf = CPF.new("29537995593")
53
+ assert_equal "295.379.955-93", cpf.formatted
54
+ end
55
+
56
+ test "generates formatted number" do
57
+ assert_match /\A\d{3}\.\d{3}\.\d{3}-\d{2}\z/, CPF.generate(true)
58
+ end
59
+
60
+ test "generates stripped number" do
61
+ assert_match /\A\d{11}\z/, CPF.generate
62
+ end
63
+
64
+ test "invalidates memoization cache" do
65
+ cpf = CPF.new("29537995593")
66
+ cpf.number = "52139989171"
67
+
68
+ assert_equal "52139989171", cpf.stripped
69
+ assert_equal "521.399.891-71", cpf.formatted
70
+ end
71
+ end