Minitest not working - ruby

I have simple ruby app that parses a string and counts the number of Xs and Os, and returns true if there is an equal number, otherwise false.
I want to know why my test isn't working.
When I run the test, the app gets called, and the test runs, but there are 0 runs, 0 assertion...etc. Why?
The app:
class Calc
def xo(str)
ex = 0
oh = 0
for i in (0...str.length)
if str[i] == "x" || str[i] == "X"
ex += 1
elsif str[i] == "o" || str[i] == "O"
oh += 1
else
end
end
if ex == oh
puts "equal"
return true
else
puts "not equal"
return false
end
end
end
Calc.new.xo("xoxoo")
The test:
require_relative "task"
require "minitest/autorun"
class Test < MiniTest::Test
def zeetest
#calc = Calc.new
assert_equal( true, #calc.xo("xoxo"))
end
end

Try this:
require_relative "task"
require "minitest/autorun"
class TestCalc < MiniTest::Test
def setup
#calc = Calc.new
end
def test_xo
assert_equal( true, #calc.xo("xoxo"))
end
end

Related

Ruby identical user defined functions return differently

only one of these functions ('render' & 'render_all') prints to terminal while the other returns "nil". I can swap them around and even make them identical. One will always work and print while the other returns "nil". I can't figure out why. I need to display all faces of my tile class at end which requires render_all and i need to render it partially while game is still playing.
class Board
def initialize(size, bombs)
#size = size
explosives = bombs
#grid = Array.new(size) {Array.new(size) {Tile.new}}
while explosives != 0
potential_bomb = #grid[rand(0...size)][rand(0...size)]
if potential_bomb.bomb == false
potential_bomb.bomb = true
potential_bomb.face = "X"
explosives -= 1
end
end
end
attr_accessor :size, :grid,
def render
grid.each do |row|
row.each do |box|
if box.shown == true
print "#{box.face}"
else
print "#"
end
end
print "\n"
end
end
def render_all
grid.each do |row|
row.each do |box|
print "#{box.face}"
end
end
end
def solved
solved = true
grid.each do |row|
row.each {|pos| solved = false if pos.shown != true && pos.bomb == false}
end
return solved
end
def [](pos)
x,y = pos
grid[x][y]
end
end
attr_accessor :size, :grid,
See that trailing comma? Is bad.
FWIW: any time there's an issue that appears order-dependent always look immediately above; it's often unrelated to the order-dependent things, rather the result of screwing something up earlier.

How to make Ruby Tester?

It asks to implement the class Tester, which receives a class and and runs all its methods that start with the word test.
class AssertionFailed < Exception
end
class TestCase
def setUp
end
def tierDown
end
def assertTrue(expresion)
if not expresion then
raise AssertionFailed.new(expresion.to_s + " is not true")
end
end
def assertEquals(result, expected)
if result != expected
raise AssertionFailed.new(result.to_s + " is not equal to " + expected.to_s)
end
end
end
class IntegerTest < TestCase
def setUp
#number = 1
end
def testSum
assertTrue(1 + #number == 2)
#number += 1
end
def testSub
assertTrue(2 - #number == #number)
end
def testMulByZero
assertEquals(#number*0, 1)
end
def testAddByZero
assertEquals(#number + 0, #number + 1)
end
end
Tester.test(IntegerTest)
Example:
Tester.test(IntegerTest)
[*] testMulByZero failed: 0 is not equals to 1
[*] testAddByZero failed: 1 is not equals to 2
Help: The grep method of the Iterable module receives a regular expression, and returns all
The elements that match that expression. For the exercise, use grep (\ test * ) on
The collection of methods to obtain the methods sought.
I finally got an answer by this source and this one
What I have done is starting the test that is given, then i create an array by asking to de class testig his instace which start with test, finaly it's an iteration on the array asking to execute eachone of the methods and if they fail the assertion then show them
class Tester
def self.test(testing)
tested=testing.new
tested.setUp
method_arr=testing.instance_methods.grep(/test*/)
method_arr.each do |x|
begin
tested.send(:"#{x}")
rescue AssertionFailed =>assert_fail
puts "[*]" + "#{assert_fail}"
end
end
end
end

how to write rspec for method

I have following code in programmability folder
module Programmability
module Parameter
class Input < Parameter::Base
attr_reader :args
def initialize(name, type, **args)
super(name, type)
#args = args
end
def value=(val)
if val
val = convert_true_false(val)
--- some code ----
end
#value = val
end
def convert_true_false(val)
return val unless #args[:limit] == 1
if [:char].include?(#type) && val == 'true'
'Y'
elsif [:char].include?(#type) && val == 'false'
'N'
elsif [:bit].include?(#type) && val == 'true'
1
elsif [:bit].include?(#type) && val == 'false'
0
end
end
end
end
end
I am trying to write rspec for method convert_true_false. I am new to rspec. any help is appreciated.
I tried doing this
context 'returns Y if passed true'
input = Programmability::Parameter::Input.new(name, type, limit)
it 'returns Y' do
name = 'unregister_series'
type = ':char'
limit = 1
expect(input.value = 'true' ).to eq('Y')
end
end
but its not picking up limit value. when it reaches convert_true_false method it comes out of it since #args[:limit] is nil
Thanks
The issue is that setters always return the value passed:
class Test
def foo=(foo)
#foo = foo + 100
return 42
end
end
puts (Test.new.foo = 69) # 69
To resolve it, simply check the value after you assigned it:
# instead of
expect(input.value = 'true').to eq 'Y'
# do
input.value = 'true'
expect(input.value).to eq 'Y'

attr_accessor - Accessing an objects attributes from another class

I want to access the ogre's object's swings attribute from the Human's class. However, all I am getting is:
NameError: undefined local variable or method ogre for
**<Human:0x007fdb452fb4f8 #encounters=3, #saw_ogre=true>
Most likely a simple solution, and my brain is just not operating this morning. I am running tests with minitest. The test and classes are below:
ogre_test.rb
def test_it_swings_the_club_when_the_human_notices_it
ogre = Ogre.new('Brak')
human = Human.new
ogre.encounter(human)
assert_equal 0, ogre.swings
refute human.notices_ogre?
ogre.encounter(human)
ogre.encounter(human)
assert_equal 1, ogre.swings
assert human.notices_ogre?
end
ogre.rb
class Ogre
attr_accessor :swings
def initialize(name, home='Swamp')
#name = name
#home = home
#encounters = 0
#swings = 0
end
def name
#name
end
def home
#home
end
def encounter(human)
human.encounters
end
def encounter_counter
#encounters
end
def swing_at(human)
#swings += 1
end
def swings
#swings
end
end
class Human
def initialize(encounters=0)
#encounters = encounters
#saw_ogre = false
end
def name
"Jane"
end
def encounters
#encounters += 1
if #encounters % 3 == 0 and #encounters != 0
#saw_ogre = true
else
#saw_ogre = false
end
if #saw_ogre == true
ogre.swings += 1 # <----issue
end
end
def encounter_counter
#encounters
end
def notices_ogre?
#saw_ogre
end
end
The easy fix would be to pass the ogre object as an argument to encounters - assuming encounters isn't used anywhere else without the argument.
class Ogre
...
def encounter(human)
human.encounters(self)
end
...
end
class Human
...
def encounters(ogre)
#encounters += 1
if #encounters % 3 == 0 and #encounters != 0
#saw_ogre = true
else
#saw_ogre = false
end
if #saw_ogre == true
ogre.swings += 1 # <----issue
end
end
...
end

Ruby: Using variables between classes

I am making a short, text-based game as an extra credit exercise based on the Ruby I have learned so far and I'm having trouble getting classes to read and write variables between each other. I have read extensively and searched for clarification on how to do this but I haven't had much luck. I have tried using # instance variables and attr_accessible but I can't figure it out. Here is my code so far:
class Game
attr_accessor :room_count
def initialize
#room_count = 0
end
def play
while true
puts "\n--------------------------------------------------"
if #room_count == 0
go_to = Entrance.new()
go_to.start
elsif #room_count == 1
go_to = FirstRoom.new()
go_to.start
elsif #room_count == 2
go_to = SecondRoom.new()
go_to.start
elsif #room_count == 3
go_to = ThirdRoom.new()
go_to.start
end
end
end
end
class Entrance
def start
puts "You are at the entrance."
#room_count += 1
end
end
class FirstRoom
def start
puts "You are at the first room."
#room_count += 1
end
end
class SecondRoom
def start
puts "You are at the second room."
#room_count += 1
end
end
class ThirdRoom
def start
puts "You are at the third room. You have reached the end of the game."
Process.exit()
end
end
game = Game.new()
game.play
I want to have the different Room classes change the #room_count variable so that Game class knows which room to go to next. I am also trying to do this without implementing class inheritance. Thanks!
class Room
def initialize(game)
#game = game
#game.room_count += 1
end
def close
#game.room_count -= 1
end
end
class Game
attr_accessor :room_count
def initialize
#room_count = 0
end
def new_room
Room.new self
end
end
game = Game.new
game.room_count # => 0
room = game.new_room
game.room_count # => 1
room.close
game.room_count # => 0

Resources