local Builder = require('snippy.builder')
local parser = require('snippy.parser')

describe('Builder', function()
    it('resolves comment vars', function()
        -- Reset commentstring to default value
        -- No long works on nightly because there is no default value
        -- vim.cmd([[set commentstring=&]])
        vim.cmd([[set commentstring=/*%s*/]])
        local builder = Builder.new()
        builder:process_structure({ { type = 'variable', name = 'BLOCK_COMMENT_START' } })
        assert.are.equal('/*', builder.result)
        builder:process_structure({ { type = 'variable', name = 'BLOCK_COMMENT_END' } })
        assert.are.equal('/**/', builder.result)
        builder.result = ''
        builder:process_structure({ { type = 'variable', name = 'LINE_COMMENT' } })
        assert.are.equal('//', builder.result)
    end)

    it('resolves comment vars from commentstring', function()
        vim.cmd([[set commentstring=--%s]])
        local builder = Builder.new()
        builder:process_structure({ { type = 'variable', name = 'LINE_COMMENT' } })
        assert.are.equal('--', builder.result)
        vim.cmd('set commentstring=--[[%s]]')
        builder.result = ''
        builder:process_structure({ { type = 'variable', name = 'BLOCK_COMMENT_START' } })
        assert.are.equal('--[[', builder.result)
        builder:process_structure({ { type = 'variable', name = 'BLOCK_COMMENT_END' } })
        assert.are.equal('--[[]]', builder.result)
    end)

    it('evaluates Vimscript', function()
        local builder = Builder.new()
        local ok, parsed, _ = parser.parse_snipmate('`len("123")`', 1)
        assert.is_true(ok)
        builder:process_structure(parsed)
        assert.are_equal('3', builder.result)
    end)

    it('evaluates Lua', function()
        local builder = Builder.new()
        local ok, parsed, _ = parser.parse_snipmate('`!lua #"123"`', 1)
        assert.is_true(ok)
        builder:process_structure(parsed)
        assert.are_equal('3', builder.result)
    end)

    it('generates UUID', function()
        local builder = Builder.new()
        builder:process_structure({ { type = 'variable', name = 'UUID' } })
        assert.match('^%x+%-%x+%-4%x+%-[89ab]%x+%-%x+$', builder.result)
    end)
end)
