Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dnd-character tests #407

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions exercises/practice/dnd-character/dnd-character_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ describe('dnd', function()
assert.equal("integer", math.type(strength))
assert.lteq(3, strength)
assert.lteq(strength, 18)
assert.equal(strength, character.strength)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is unnecessary because strength is initialized with character.strength above.

Copy link
Contributor

@keiravillekode keiravillekode Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem-specifications say to check "each ability is only calculated once"

Here is a solution we want to reject:

function Character:new(name)
  self.__index = function(mytable, key)
    return ability(roll_dice())
  end

  local meta_tbl = {
    name = name,
    constitution = ability(roll_dice())
  }
  meta_tbl.hitpoints = 10 + modifier(meta_tbl.constitution)

  return setmetatable(meta_tbl, self)
end

Instead of three tests

      "description": "random ability is within range",
      "description": "random character is valid",
      "description": "each ability is only calculated once",

I wrote one test for each ability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I put them back.

end)

it('creates a character with valid dexterity', function()
Expand All @@ -131,7 +130,6 @@ describe('dnd', function()
assert.equal("integer", math.type(dexterity))
assert.lteq(3, dexterity)
assert.lteq(dexterity, 18)
assert.equal(dexterity, character.dexterity)
end)

it('creates a character with valid constitution', function()
Expand All @@ -140,7 +138,6 @@ describe('dnd', function()
assert.equal("integer", math.type(constitution))
assert.lteq(3, constitution)
assert.lteq(constitution, 18)
assert.equal(constitution, character.constitution)
end)

it('creates a character with valid intelligence', function()
Expand All @@ -149,7 +146,6 @@ describe('dnd', function()
assert.equal("integer", math.type(intelligence))
assert.lteq(3, intelligence)
assert.lteq(intelligence, 18)
assert.equal(intelligence, character.intelligence)
end)

it('creates a character with valid wisdom', function()
Expand All @@ -158,7 +154,6 @@ describe('dnd', function()
assert.equal("integer", math.type(wisdom))
assert.lteq(3, wisdom)
assert.lteq(wisdom, 18)
assert.equal(wisdom, character.wisdom)
end)

it('creates a character with valid charisma', function()
Expand All @@ -167,13 +162,12 @@ describe('dnd', function()
assert.equal("integer", math.type(charisma))
assert.lteq(3, charisma)
assert.lteq(charisma, 18)
assert.equal(charisma, character.charisma)
end)

it('creates a character with valid hitpoints', function()
for i = 1, 10 do
local character = dnd.Character:new(tostring(i))
assert.equal(character.hitpoints, 10 + dnd.modifier(character.constitution))
assert.equal(10 + dnd.modifier(character.constitution), character.hitpoints)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order must be expected, actual in order for the error message to be correct.

end
end)
end)
Expand Down