#!/usr/bin/env lua
-- Unit tests for Lua pi calculation using busted

local SCRIPT_PATH = "/Users/einand/Code/test/lua/print_hej.lua"

local function run_script(...)
    local args = {...}
    local cmd = "lua " .. SCRIPT_PATH
    for _, arg in ipairs(args) do
        cmd = cmd .. " " .. tostring(arg)
    end
    
    local handle = io.popen(cmd, "r")
    local result = handle:read("*a")
    handle:close()
    
    -- Trim newline
    result = result:gsub("\n$", "")
    return result
end

describe("Pi calculation", function()
    it("calculates 10 decimals", function()
        local result = run_script(10)
        local expected = "3.1415926535"
        assert.are.equal(expected, result)
    end)
    
    it("calculates 5 decimals", function()
        local result = run_script(5)
        local expected = "3.14159"
        assert.are.equal(expected, result)
    end)
    
    it("calculates 1 decimal", function()
        local result = run_script(1)
        local expected = "3.1"
        assert.are.equal(expected, result)
    end)
    
    it("calculates 100 decimals", function()
        local result = run_script(100)
        local expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
        assert.are.equal(expected, result)
    end)
    
    it("calculates default 100 decimals", function()
        local result = run_script()
        local expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
        assert.are.equal(expected, result)
    end)
    
    it("calculates 10000 decimals", function()
        local result = run_script(10000)
        -- Check length: "3." + 10000 digits = 10002 characters
        assert.are.equal(10002, #result)
        assert.is_true(string.sub(result, 1, 7) == "3.14159")
    end)
end)