First Small Step Into Testing
I am working on Exercise 28 of Learn Ruby the Hard Way and this is my first concrete example of using testing in the course of coding.
The exercise is a test of boolean logic: I am given a statement e.g.
And must work out whether it is "true" or "false".
As you can see, I have organised the statements in a two-dimensional array in which each sub-array is composed of three elements: a string denoting the statement, the statement itself, and my prediction.
The exercise is a test of boolean logic: I am given a statement e.g.
true == true
And must work out whether it is "true" or "false".
As you can see, I have organised the statements in a two-dimensional array in which each sub-array is composed of three elements: a string denoting the statement, the statement itself, and my prediction.
# List of boolean statements to test my knowledge thereof, organised in a two-dimensional arrayI test my prediction by running the following testing loop, and in keeping with the practice of "green" for good and "red" for error, I installed the colorize gem and applied it to the output of the testing loop.
booleanTest = [
["true && true", true && true, true],
["false && true", false && true, false],
["1 == 1 && 2 == 1", 1 == 1 && 2 == 1, false],
['"test" == "test"', "test" == "test", true],
["1 == 1 || 2 != 1", 1 == 1 || 2 != 1, true],
["true && 1 == 1", true && 1 == 1, true],
["false && 0 != 0", false && 0 != 0, false],
["true || 1 == 1", true || 1 == 1, true],
['"test" == "testing"', "test" == "testing", false],
["1 != 0 && 2 == 1", 1 != 0 && 2 == 1, false],
['"test" != "testing"', "test" != "testing", true],
['"test" == 1', "test" == 1, false],
["!(true && false)", !(true && false), true],
["!(1 == 1 && 0 != 1)", !(1 == 1 && 0 != 1), false],
["!(10 == 1 || 1000 == 1000)", !(10 == 1 || 1000 == 1000), false],
["!(1 != 10 || 3 == 4)", !(1 != 10 || 3 == 4), true]
]
# Required in order to highlight the text in the testing loop green or red.And here are the results:
# Colorize gem to mark the result green or red.
require 'colorize'
# Testing loop
puts "Here are the results of my test:\n\n"
booleanTest.each do|description,result,prediction|
puts "PREDICTION: #{description} GIVES #{prediction}, RESULT: #{result}"
if prediction == result
puts "Correct!".colorize(:green)
else
puts "Incorrect!".colorize(:red)
end
end
Published on 08 Dec 2016
by Alexander Garber
all tags
100daysofcode activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career ci-cd cli cloud code coding config configuration cp crud cryptography css csv database db design devops django docker email erp feelsgood filter fugitive gif gist git gnome gnome pomodoro grep hebrew http ide isbn-fetcher iso javascript job search js kanban kindle koans learning linkedin linux logger manjaro map markdown microservices mobi mtp neovim nodejs nvchad packages panda pastbin patch portfolio post postgres pytest python rails reduce refactoring reflections rest routes rspec ruby salesforce script scripting security sed shell sql string_replacement study tdd terminal testing tmux ttd version_control vim vim sort walkthrough webdev workflow zsh