Top Level Namespace

Defined Under Namespace

Modules: Status Classes: Colors, Result

Constant Summary collapse

USAGE =
'# **USAGE**
```py
test(challenge: int, your_function: :func, description: bool, examples: bool)
---
test(458, :your_function)   # runs all tests cases on your function
test(458, :your_function, description: true)  # adds the test description in the terminal
test(458, :your_function, examples: true)  # adds test examples in the terminal
test(458, :your_function, true, true)  # adds both description and examples
test(458, description: true)  # prints only description without running tests
test(458, examples: true)  # same as above but with examples```'

Instance Method Summary collapse

Instance Method Details

#fetch_data(url) ⇒ Object



70
71
72
# File 'lib/challenges.rb', line 70

def fetch_data(url)
  Net::HTTP.get(URI(url)).force_encoding('UTF-8')
end

#get_info(challenge, description, examples) ⇒ Object

fetches the description from source



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/challenges.rb', line 86

def get_info(challenge, description, examples)
  url = "https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/challenge_#{challenge}.md"
  result = ''

  begin
    info = fetch_data(url)
    result += info.split('## ').first.gsub("\n\n", "\n") << "\n" if description
    result += info.split('#')[3].gsub("\n\n", "\n").insert(0,'#') if examples
  rescue IndexError || RuntimeError => e
    result += e
  end
  TTY::Markdown.parse(result + '***')
end

#get_tests(challenge) ⇒ Object

fetches the result from the source



75
76
77
78
79
80
81
82
83
# File 'lib/challenges.rb', line 75

def get_tests(challenge)
  url = "https://raw.githubusercontent.com/beginner-codes/challenges/main/weekday/test_cases_#{challenge}.json"
  begin
    JSON.parse(fetch_data(url))
  rescue RuntimeError => e
    e
  end

end

#make_table(rows) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/challenges.rb', line 61

def make_table(rows)
  c = Colors.new
  header = [c.blue('#:'), c.green('Expected:'), c.red('Got:')]
  rows = rows.map do |row|
    [c.blue(row[0]), c.green(row[1]), c.red(row[2])]
  end
  TTY::Table.new(header, [*rows])
end

#run_tests(tests, solution_func) ⇒ Object

parses your functions and checks if it passes



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/challenges.rb', line 101

def run_tests(tests, solution_func)
  results = []
  tests.each_with_index do |test_case, index|
    result = Result.new(index, test_case['return'])
    begin
      result.got = solution_func.is_a?(Proc) ? solution_func.call(*test_case['args']) : send(solution_func, *test_case['args'])
    rescue StandardError => e
      result.status = Status::EXCEPTION
      result.got = e
    else
      result.status = Status::FAILED unless result.got.eql?(test_case['return'])
    end
    results << result
  end
  results
end

#show_results(challenge, results, total_tests, info) ⇒ Object

displays the results on the terminal



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/challenges.rb', line 119

def show_results(challenge, results, total_tests, info)
  puts info
  c = Colors.new
  rows = []
  failures = 0
  results.each do |result|
    next unless result.status.equal?(Status::FAILED) or result.status.equal?(Status::EXCEPTION)

    rows << [result.index, result.expected.inspect, result.got]
    failures += 1
  end

  table = make_table(rows)
  puts table.render(:unicode, alignment: %i[right left left]) if failures
  return if results == []

  puts c.yellow("---- Challenge #{challenge} Results ----")
  puts "#{c.green(total_tests - failures)} passed, #{c.red(failures)} failed"
  puts c.green("\n**** Great job!!! ****") if failures.zero?
end

#test(challenge = nil, solution_func = nil, description: false, examples: false) ⇒ Object

the main entry point function



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/challenges.rb', line 141

def test(challenge = nil, solution_func = nil, description: false, examples: false)
  if solution_func
    tests = get_tests(challenge)
    results = run_tests(tests, solution_func) unless tests.kind_of? RuntimeError
  else
    tests = []
    results = []
  end
  info = if solution_func || examples || description || challenge
           get_info(challenge, description, examples)
         else
           TTY::Markdown.parse(USAGE)
         end
  show_results(challenge, results, tests.size, info)
end