beginner.codes 0.1.5 → 0.1.6

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/README.md +1 -0
  4. data/lib/challenges.rb +95 -39
  5. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff8827c1454a8d44b7ebd23a4c6c2b0c6f73e6502643630b0de30434d54ca424
4
- data.tar.gz: f845377f0f7e3e4ce5a5446fcea9196c9d3ebe411b778c7708c508f200de5c3c
3
+ metadata.gz: cb2311d48b3a619a02565b5d4038172500fc7aff654a7af3d9085732e38d0294
4
+ data.tar.gz: 700302fc4e435f889df81f5c20ab929bdb80fd1057e2d257b0db00ae5f5fab91
5
5
  SHA512:
6
- metadata.gz: ca8ca526d9026fcb0fe9745c96f801c9e210a8c761cbb973014d644d3c3ccdb5dd9426e52d5e2b03488bacf38afc290b79ded83db2f7c9bfe8f41e89b2157af5
7
- data.tar.gz: 9fff417792131a316f81e47ecd08e29798f8d0d3ff548aeda9de2457c2e18f2257c1c431614dfdcaeff9434015359f4564e65333aeb51fedec5731c070923d07
6
+ metadata.gz: 56ea71d0fe3926cfff3618efed0949ef332a693eba65691777729cff91bf37754646b0021d9c876f4b3a1e6e6e917c4effccf26604e395d47442690d5b8c3458
7
+ data.tar.gz: 15eabb9b8816636c9e6e1843e8592ebf69237a3e5f4b47739eb637ac0dd1347a06ec4bf671a2a8fb45b71c3ec812e52d15b33694377d7a9f87deee43f6296949
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
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
+ ---
1
21
  # 0.1.5 (9 November 2023)
2
22
 
3
23
  ---
data/README.md CHANGED
@@ -7,6 +7,7 @@ This is the un-official Ruby Gem for the Beginner.Codes Discord server.
7
7
  - Install the package: `gem install beginner.codes`
8
8
  - Import the test runner: `require 'challenges'`
9
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
 
data/lib/challenges.rb CHANGED
@@ -1,13 +1,24 @@
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'
7
8
  require 'tty-markdown'
9
+ require 'tty-table'
8
10
 
9
- TEST_CASE_URL = 'https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/test_cases_'
10
- DESCRIPTION_URL = 'https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/challenge_'
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```'
11
22
 
12
23
  module Status
13
24
  SUCCESS = 1
@@ -15,6 +26,25 @@ module Status
15
26
  EXCEPTION = -1
16
27
  end
17
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
+
18
48
  # captures the result of the test
19
49
  class Result
20
50
  attr_accessor :got, :status
@@ -28,25 +58,43 @@ class Result
28
58
  end
29
59
  end
30
60
 
31
- # fetches the description from source
32
- def puts_info(challenge, description, examples)
33
- url = URI(DESCRIPTION_URL + "#{challenge}.md")
34
- response = Net::HTTP.get(url).force_encoding('UTF-8')
35
- result = "\n"
36
- result += response.split('## ').first.gsub("\n\n", "\n")+"\n" if description
37
- result += response.split('#')[3].gsub("\n\n", "\n").insert(0,'#') if examples
38
- result = TTY::Markdown.parse(result)
39
- puts result
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')
40
72
  end
41
73
 
42
74
  # fetches the result from the source
43
75
  def get_tests(challenge)
44
- uri = URI(TEST_CASE_URL + "#{challenge}.json")
45
- response = Net::HTTP.get_response(uri)
46
- 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
82
+
83
+ end
47
84
 
48
- JSON.parse(response.body)
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 = ''
49
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 + '***')
50
98
  end
51
99
 
52
100
  # parses your functions and checks if it passes
@@ -55,12 +103,7 @@ def run_tests(tests, solution_func)
55
103
  tests.each_with_index do |test_case, index|
56
104
  result = Result.new(index, test_case['return'])
57
105
  begin
58
- result.got = if solution_func.is_a?(Proc)
59
- solution_func.call(*test_case['args'])
60
- else
61
- send(solution_func, *test_case['args'])
62
- end
63
-
106
+ result.got = solution_func.is_a?(Proc) ? solution_func.call(*test_case['args']) : send(solution_func, *test_case['args'])
64
107
  rescue StandardError => e
65
108
  result.status = Status::EXCEPTION
66
109
  result.got = e
@@ -73,27 +116,40 @@ def run_tests(tests, solution_func)
73
116
  end
74
117
 
75
118
  # displays the results on the terminal
76
- 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 = []
77
123
  failures = 0
78
124
  results.each do |result|
79
- if result.status.equal?(Status::FAILED)
80
- puts "Test #{result.index.to_s.blue} failed: Expected #{result.expected.to_s.green}, got #{result.got.to_s.red}"
81
- failures += 1
82
- elsif result.status.equal?(Status::EXCEPTION)
83
- puts "Test #{result.index} failed: #{result.got}"
84
- failures += 1
85
- 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
86
129
  end
87
- puts ' ' if failures
88
- puts "---- Challenge #{challenge} Results ----".light_yellow.on_black
89
- puts "#{(total_tests - failures).to_s.green} passed, #{failures.to_s.red} failed"
90
- 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?
91
138
  end
92
139
 
93
140
  # the main entry point function
94
- def test(challenge, solution_func, description: false, examples: false)
95
- tests = get_tests(challenge)
96
- puts_info(challenge, description, examples) if description || examples
97
- results = run_tests(tests, solution_func)
98
- 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)
99
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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mr. Robinhood 5
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-11-10 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
28
  - !ruby/object:Gem::Dependency
29
29
  name: tty-markdown
30
30
  requirement: !ruby/object:Gem::Requirement