class Person
attr_accessor :name
def initialize(name)
@name = name
end
end
def change_name(new_name, thing)
if thing.respond_to?(:name=)
# do something
thing.name = new_name
else
nil
end
end
bob = Person.new("bob")
person_class = Person
number_three = 3
Test.assert_equals(change_name("Robert", bob), "Robert", "bob.name is set to Robert as attr_accessor creates :name and :name= methods")
Test.assert_equals(change_name("The Person Class", person_class), nil, "nil as Class doesn't have a name= method")
Test.assert_equals(change_name("Three", number_three), nil, "nil as 3 doesn't have a name= method")
def array_of_two(n)
raise 'n is not a number' unless n.is_a?(Numeric) # comment to fail test case
[n, n]
end
describe "Test#expect_error" do
Test.expect_error("array_of_two should raise an exception if N is not a number") { array_of_two('hello world') }
end
// const request = require('request');
// function getUserAllies(username){
// return new Promise(function(resolve, reject){
// request('https://www.codewars.com/users/'+username, function(err, res, body){
// let html = body;
// let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
// resolve(+allies);
// });
// });
// }
// using request-promise to avoid wrapping a Promise
const rp = require('request-promise');
function getUserAllies(username){
return rp('https://www.codewars.com/users/'+username).then(function(body){
let html = body;
let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
return(+allies);
});
}
// might fail because allies can change.
let users = [['myjinxin2015', 19], ['g964', 617], ['Voile', 206], ['SteffenVogel_79', 46], ['smile67', 20]];
let arr = users.map(user=>[getUserAllies(user[0]), user[1], user[0]]);
describe('getUserAllies', function(){
for (let i = 0; i < arr.length; i++){
let [userAns, allies, user] = arr[i];
userAns.then(data => Test.assertEquals(data, allies, `Incorrect numbers of allies for ${user}!`));
}
});
Use snippets in Test Cases and Preloaded sections.
# Uncomment method redefinitions and errors will be thrown(can't publish with errors). (ノ^_^)ノ┻━┻
class NilClass
# def ==(o)
# true
# end
# def !=(o)
# true
# end
end
class Object
# def ==(o)
# true
# end
# def !=(o)
# true
# end
end
def method
# nil
end
describe "Cheat Detection" do
it "should return false" do
Test.assert_not_equals(method, '')
Test.assert_not_equals(method, 1)
end
it "should return true" do
Test.assert_equals(method, nil)
Test.assert_equals(method, nil)
end
end
const package = require('fs').readFileSync('../../runner/package.json','utf8'); console.log(package);
const exec = require('child_process').exec;- const package = require('fs').readFileSync('../../runner/package.json','utf8');
function parse(error, stdout, stderr) {if (error) throw error;console.log(`stdout: ${stdout}`);console.log(`stderr: ${stderr}`);}// gives an error I don't understand but I guess the below command sort of works.// exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr));exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));- console.log(package);
const html = '<iframe width=100% height=640 src="https://stemkoski.github.io/Three.js/Particle-Engine.html"></iframe>';
console.log(html);
Read solution.txt
in preloaded section so the user's solution is read before it's modified.
// write a add function that doesn't use the plus symbol function add(a, b){ return a + b; } // Make sure to view the test cases to see how this test fails // Rewrite solution.txt require('fs').writeFileSync('/home/codewarrior/solution.txt', 'no pluses here!')
- // write a add function that doesn't use the plus symbol
- function add(a, b){
- return a + b;
- }
// Make sure to view the test cases to see how this test fails- // Make sure to view the test cases to see how this test fails
- // Rewrite solution.txt
- require('fs').writeFileSync('/home/codewarrior/solution.txt', 'no pluses here!')
const fs = require('fs'); const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8'); describe("Check Solution", function(){ it("should prevent the '+' symbol from being used anywhere in the code", function(){ Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!"); }); }); describe("Check Real Solution", function(){ it("should prevent the '+' symbol from being used anywhere in the code", function(){ Test.expect(realSolution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!"); }); });
- const fs = require('fs');
- const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');
- describe("Check Solution", function(){
- it("should prevent the '+' symbol from being used anywhere in the code", function(){
- Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");
- });
- });
- describe("Check Real Solution", function(){
- it("should prevent the '+' symbol from being used anywhere in the code", function(){
- Test.expect(realSolution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");
- });
- });
const exec = require('child_process').exec; function parse(error, stdout, stderr) { if (error) throw error; console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`); } // gives an error I don't understand but I guess the below command sort of works. // exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr)); exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));
from subprocess import callcall(["pip", "freeze"])- const exec = require('child_process').exec;
- function parse(error, stdout, stderr) {
- if (error) throw error;
- console.log(`stdout: ${stdout}`);
- console.log(`stderr: ${stderr}`);
- }
- // gives an error I don't understand but I guess the below command sort of works.
- // exec('npm -g list', (err, stdout, stderr) => parse(err, stdout, stderr));
- exec('ls ../../runner/node_modules', (err, stdout, stderr) => parse(err, stdout, stderr));
from subprocess import call
call(["pip", "freeze"])
# write a add function that doesn't use the plus symbol def add(a, b) a + b end # Make sure to view the test cases to see how this test fails
// write a add function that doesn't use the plus symbolfunction add(a, b){return a + b;}- # write a add function that doesn't use the plus symbol
- def add(a, b)
- a + b
- end
// Make sure to view the test cases to see how this test fails- # Make sure to view the test cases to see how this test fails
solution = File.open('/home/codewarrior/solution.txt', 'r').read describe "Check Solution" do it "should prevent the '+' symbol from being used anywhere in the code" do Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!") end end
const fs = require('fs');const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');- solution = File.open('/home/codewarrior/solution.txt', 'r').read
describe("Check Solution", function(){it("should prevent the '+' symbol from being used anywhere in the code", function(){Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");});});- describe "Check Solution" do
- it "should prevent the '+' symbol from being used anywhere in the code" do
- Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!")
- end
- end
Test output documentation: https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes
If you print '<COMPLETEDIN::>'
after a Test.describe() or Test.it()
, you can close describe/it groupings and have a nested structure like you can for the other languages.
def hi():
return 'hi'
# https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes
# use this to close Test.describe and Test.it groupings
def close_group():
print('<COMPLETEDIN::>')
close_describe = close_it = close_group;
Test.describe('Hola')
Test.it('should return "hi"')
Test.assert_equals(hi(), 'hi')
close_it()
close_describe()
Test.describe('This test is not nested under previous describe')
Test.it('should return "hola"')
Test.assert_equals(hi(), "hi")
close_it()
close_describe()
Test.describe('Test nested "it\'s"')
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
close_describe()
Test.describe('Test nested describes')
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
def solution (nums) # more ruby-ish, two passes after generating permutations. nums.permutation.map(&:join).max end
- def solution (nums)
# slower but simple ;-)nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0]- # more ruby-ish, two passes after generating permutations.
- nums.permutation.map(&:join).max
- end