lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.39k stars 184 forks source link

[BUG] same-same, but still different #671

Closed Kochise closed 2 years ago

Kochise commented 2 years ago

Hi, having a problem with an assert :

    local mock_file = {}

    local mock_pro = {}
    mock_pro.set_uuid = function(self, uuid) self.UUID = uuid end
    mock_pro.set_ssid = function(self, ssid) self.SSID = ssid end
    mock_pro.set_password = function(self, password) self.password = password end
    mock_pro.set_ipv4_mode = function(self, mode) self.ipv4_mode = mode end
    mock_pro.set_connected = function(self, connected) self.connected = connected end

    local mock_json = {}
    mock_json.set_uuid = function(self, uuid) self.UUID = uuid end
    mock_json.set_ssid = function(self, ssid) self.SSID = ssid end
    mock_json.set_password = function(self, password) self.password = password end
    mock_json.set_ipv4_mode = function(self, mode) self.ipv4_mode = mode end
    mock_json.set_connected = function(self, connected) self.connected = connected end
    mock_json.UUID = '0'
    --mock_json.SSID = 'SSID'
    --mock_json.password = 'PSK'
    mock_json.ipv4_mode = 'dhcp'
    --mock_json.connected = false

    it("returns {mock_json} when configuration file is empty", function()
        -- fault (l89)
        -- pass (l54)
        io.open.on_call_with(match._).returns(mock_file, constants.WIFI_CFG_FILE)
        -- fault (l59)
        mock_file.lines = function(self) return {} end
        -- pass (l68)
        mock_file.close = function(self) return true, 0 end
        -- pass (l91)
        wifi_profile.new.on_call_with(match._).returns(mock_pro)

        -- check (l99)
        assert.are.same(unit_under_test.get_wifi_profiles(), {mock_json})
    end)

Result :

WiFi manager returns {mock_json} when configuration file is empty
test_wifi_manager.lua:104: Expected objects to be the same.
Passed in:
(table: 0xff078a98) {
 *[1] = {
    [UUID] = '0'
    [ipv4_mode] = 'dhcp'
    [set_connected] = function: 0xff5db600
    [set_ipv4_mode] = function: 0xff5db5e8
   *[set_password] = function: 0xff5db5d0
    [set_ssid] = function: 0xff5db5b8
    [set_uuid] = function: 0xff5db5a0 } }
Expected:
(table: 0xff078a58) {
 *[1] = {
    [UUID] = '0'
    [ipv4_mode] = 'dhcp'
    [set_connected] = function: 0xff5db498
    [set_ipv4_mode] = function: 0xff5db480
   *[set_password] = function: 0xff5db400
    [set_ssid] = function: 0xff5db3e8
    [set_uuid] = function: 0xff5db398 } }

And the obligatory meme :

same

Regards.

Tieske commented 2 years ago

seems like a new closure is created, hence it is different. Seems like standard Lua behaviour, but can be tricky to test.

Kochise commented 2 years ago

Damn, I feel so relieved...

alerque commented 2 years ago

Unless I'm missing something here this appears to be all as expected. The closure is indeed a new one ever if the contents are similar. If there is something else to consider here comment for re-evaluation.