beginner.codes 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/README.md +8 -2
  4. data/lib/challenges.rb +98 -27
  5. metadata +19 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53a61ed7bdf941612a9b872243a1735c99e6510a0f787488654fd5cb0f57183e
4
- data.tar.gz: 749500774942cd631b2c7f072f42e0bd6ed703791a8d97fdbec0a8b34367928e
3
+ metadata.gz: cb2311d48b3a619a02565b5d4038172500fc7aff654a7af3d9085732e38d0294
4
+ data.tar.gz: 700302fc4e435f889df81f5c20ab929bdb80fd1057e2d257b0db00ae5f5fab91
5
5
  SHA512:
6
- metadata.gz: 944c7ac5de055f6f919537b62525ee86135c50add2fbbabe53cfd95e58bc39db9aadf5718504ce0bd7b41396279649824a13b733a2b4c9dc8453955482f97ad2
7
- data.tar.gz: ea7009f4fc91cfdb3d339e9d676138ca0f98c12d96762ca2a65f876810557c3d4d4098563752dc96250dd92af575d42a20eef5a199f19d37df2f89ab889a584e
6
+ metadata.gz: 56ea71d0fe3926cfff3618efed0949ef332a693eba65691777729cff91bf37754646b0021d9c876f4b3a1e6e6e917c4effccf26604e395d47442690d5b8c3458
7
+ data.tar.gz: 15eabb9b8816636c9e6e1843e8592ebf69237a3e5f4b47739eb637ac0dd1347a06ec4bf671a2a8fb45b71c3ec812e52d15b33694377d7a9f87deee43f6296949
data/CHANGELOG.md CHANGED
@@ -1,3 +1,42 @@
1
+ # 0.1.6 (11 November 2023)
2
+
3
+ ---
4
+
5
+ ## Enhancements
6
+
7
+ - Using better colors and now tables for results
8
+ - Prints Usage instructions if wrong arguments are sent
9
+ - passing a function is now optional, in case you only need the test info.
10
+
11
+ ## Bug Fixes
12
+
13
+ ## Performance
14
+
15
+ ## Documentation
16
+
17
+ - Updated README to include the new arguments
18
+ - drip drop drip drop
19
+
20
+ ---
21
+ # 0.1.5 (9 November 2023)
22
+
23
+ ---
24
+
25
+ ## Enhancements
26
+
27
+ - Added two arguments passed to test `description: true` and `examples: true` to print out additional challenge data during testing
28
+
29
+ ## Bug Fixes
30
+
31
+ ## Performance
32
+
33
+ ## Documentation
34
+
35
+ - Updated README to include the new arguments
36
+ - Thanksgiving approaches along with the cruel realization that I never lost the weight from last new years.
37
+
38
+ ---
39
+
1
40
  # 0.1.4 (7 November 2023)
2
41
 
3
42
  ---
data/README.md CHANGED
@@ -6,7 +6,8 @@ This is the un-official Ruby Gem for the Beginner.Codes Discord server.
6
6
 
7
7
  - Install the package: `gem install beginner.codes`
8
8
  - Import the test runner: `require 'challenges'`
9
- - Run the tests, passing in the challenge number and your solution function: `test(458, n_differences)`
9
+ - Run the tests, passing in the challenge number and your solution function: `test(458, :n_differences)` for a function or `test(458, n_differences)` for a lambda function
10
+ - For informatino only you can pass either `description: true` or `examples: true`
10
11
  ```ruby
11
12
  require 'challenges'
12
13
 
@@ -22,4 +23,9 @@ n_differences = -> (x) {x*2}
22
23
 
23
24
  test(458, n_differences)
24
25
  ```
25
- This will handle downloading the necessary challenge test cases and will run them against your code. It will show you which tests failed, what went wrong, and how many tests succeeded.
26
+ This will handle downloading the necessary challenge test cases and will run them against your code. It will show you which tests failed, what went wrong, and how many tests succeeded.
27
+
28
+ Additionally, you can view the description and examples when running the tests by adding some options to the test function
29
+ ```ruby
30
+ test(458, :n_differences, description: true, examples: true)
31
+ ```
data/lib/challenges.rb CHANGED
@@ -1,17 +1,50 @@
1
1
  # frozen_string_literal: true
2
+ # TODO: use a latest: arg to bring up the last test
2
3
 
3
4
  require 'json'
4
5
  require 'uri'
5
6
  require 'net/http'
6
- require 'colorize'
7
+ require 'pastel'
8
+ require 'tty-markdown'
9
+ require 'tty-table'
7
10
 
8
11
 
12
+ USAGE = '# **USAGE**
13
+ ```py
14
+ test(challenge: int, your_function: :func, description: bool, examples: bool)
15
+ ---
16
+ test(458, :your_function) # runs all tests cases on your function
17
+ test(458, :your_function, description: true) # adds the test description in the terminal
18
+ test(458, :your_function, examples: true) # adds test examples in the terminal
19
+ test(458, :your_function, true, true) # adds both description and examples
20
+ test(458, description: true) # prints only description without running tests
21
+ test(458, examples: true) # same as above but with examples```'
22
+
9
23
  module Status
10
24
  SUCCESS = 1
11
25
  FAILED = 0
12
26
  EXCEPTION = -1
13
27
  end
14
28
 
29
+ # passes the colors to the terminal
30
+ class Colors
31
+ def initialize
32
+ @pastel = Pastel.new
33
+ end
34
+
35
+ def respond_to_missing?(name, include_private = false)
36
+ !name.to_s.empty? || super
37
+ end
38
+
39
+ def method_missing(name, *args)
40
+ if respond_to_missing?(name)
41
+ @pastel.send(name, *args)
42
+ else
43
+ @pastel.send(:white, *args)
44
+ end
45
+ end
46
+ end
47
+
15
48
  # captures the result of the test
16
49
  class Result
17
50
  attr_accessor :got, :status
@@ -25,14 +58,43 @@ class Result
25
58
  end
26
59
  end
27
60
 
61
+ def make_table(rows)
62
+ c = Colors.new
63
+ header = [c.blue('#:'), c.green('Expected:'), c.red('Got:')]
64
+ rows = rows.map do |row|
65
+ [c.blue(row[0]), c.green(row[1]), c.red(row[2])]
66
+ end
67
+ TTY::Table.new(header, [*rows])
68
+ end
69
+
70
+ def fetch_data(url)
71
+ Net::HTTP.get(URI(url)).force_encoding('UTF-8')
72
+ end
73
+
28
74
  # fetches the result from the source
29
75
  def get_tests(challenge)
30
- uri = URI("https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/test_cases_#{challenge}.json")
31
- response = Net::HTTP.get_response(uri)
32
- raise "Challenge #{challenge} was not found" unless response.is_a?(Net::HTTPSuccess)
76
+ url = "https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/test_cases_#{challenge}.json"
77
+ begin
78
+ JSON.parse(fetch_data(url))
79
+ rescue RuntimeError => e
80
+ e
81
+ end
33
82
 
34
- JSON.parse(response.body)
83
+ end
84
+
85
+ # fetches the description from source
86
+ def get_info(challenge, description, examples)
87
+ url = "https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/challenge_#{challenge}.md"
88
+ result = ''
35
89
 
90
+ begin
91
+ info = fetch_data(url)
92
+ result += info.split('## ').first.gsub("\n\n", "\n") << "\n" if description
93
+ result += info.split('#')[3].gsub("\n\n", "\n").insert(0,'#') if examples
94
+ rescue IndexError || RuntimeError => e
95
+ result += e
96
+ end
97
+ TTY::Markdown.parse(result + '***')
36
98
  end
37
99
 
38
100
  # parses your functions and checks if it passes
@@ -41,12 +103,7 @@ def run_tests(tests, solution_func)
41
103
  tests.each_with_index do |test_case, index|
42
104
  result = Result.new(index, test_case['return'])
43
105
  begin
44
- result.got = if solution_func.is_a?(Proc)
45
- solution_func.call(*test_case['args'])
46
- else
47
- send(solution_func, *test_case['args'])
48
- end
49
-
106
+ result.got = solution_func.is_a?(Proc) ? solution_func.call(*test_case['args']) : send(solution_func, *test_case['args'])
50
107
  rescue StandardError => e
51
108
  result.status = Status::EXCEPTION
52
109
  result.got = e
@@ -59,26 +116,40 @@ def run_tests(tests, solution_func)
59
116
  end
60
117
 
61
118
  # displays the results on the terminal
62
- def show_results(challenge, results, total_tests)
119
+ def show_results(challenge, results, total_tests, info)
120
+ puts info
121
+ c = Colors.new
122
+ rows = []
63
123
  failures = 0
64
124
  results.each do |result|
65
- if result.status.equal?(Status::FAILED)
66
- puts "Test #{result.index.to_s.blue} failed: Expected #{result.expected.to_s.green}, got #{result.got.to_s.red}"
67
- failures += 1
68
- elsif result.status.equal?(Status::EXCEPTION)
69
- puts "Test #{result.index} failed: #{result.got}"
70
- failures += 1
71
- end
125
+ next unless result.status.equal?(Status::FAILED) or result.status.equal?(Status::EXCEPTION)
126
+
127
+ rows << [result.index, result.expected, result.got]
128
+ failures += 1
72
129
  end
73
- puts ' ' if failures
74
- puts "---- Challenge #{challenge} Results ----".light_yellow.on_black
75
- puts "#{(total_tests - failures).to_s.green} passed, #{failures.to_s.red} failed"
76
- puts "\n**** Great job!!! ****".green if failures.zero?
130
+
131
+ table = make_table(rows)
132
+ puts table.render(:unicode, alignment: %i[right left left]) if failures
133
+ return if results == []
134
+
135
+ puts c.yellow("---- Challenge #{challenge} Results ----")
136
+ puts "#{c.green(total_tests - failures)} passed, #{c.red(failures)} failed"
137
+ puts c.green("\n**** Great job!!! ****") if failures.zero?
77
138
  end
78
139
 
79
140
  # the main entry point function
80
- def test(challenge, solution_func)
81
- tests = get_tests(challenge)
82
- results = run_tests(tests, solution_func)
83
- show_results(challenge, results, tests.size)
141
+ def test(challenge = nil, solution_func = nil, description: false, examples: false)
142
+ if solution_func
143
+ tests = get_tests(challenge)
144
+ results = run_tests(tests, solution_func) unless tests.kind_of? RuntimeError
145
+ else
146
+ tests = []
147
+ results = []
148
+ end
149
+ info = if solution_func || examples || description || challenge
150
+ get_info(challenge, description, examples)
151
+ else
152
+ TTY::Markdown.parse(USAGE)
153
+ end
154
+ show_results(challenge, results, tests.size, info)
84
155
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beginner.codes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mr. Robinhood 5
@@ -9,22 +9,36 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-11-08 00:00:00.000000000 Z
12
+ date: 2023-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: colorize
15
+ name: pastel
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: 1.1.0
20
+ version: 0.8.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: 1.1.0
27
+ version: 0.8.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: tty-markdown
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.7.2
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.7.2
28
42
  description: This is the un-official RubyGem for the Beginner.Codes Discord server.
29
43
  Test your daily challenge solutions with provided tests automatically to ensure
30
44
  you have a good solution.