# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

source ../macports_test_autoconf.tcl
package require macports 1.0


test unobscure_maintainers_github {
    Test macports::unobscure_maintainers with a GitHub handle
} -body {
    return [macports::unobscure_maintainers {@github-user}]
} -result {{github github-user}}

test unobscure_maintainers_email {
    Test macports::unobscure_maintainers with unobfuscated email addresses
} -body {
    return [macports::unobscure_maintainers {localpart@example.com}]
} -result {{email localpart@example.com}}

test unobscure_maintainers_obfuscated_email {
    Test macports::unobscure_maintainers with obfuscated email addresses
} -body {
    return [macports::unobscure_maintainers {example.com:localpart}]
} -result {{email localpart@example.com}}

test unobscure_maintainers_handle {
    Test macports::unobscure_maintainers with MacPorts handles
} -body {
    return [macports::unobscure_maintainers {handle}]
} -result {{email handle@macports.org}}

test unobscure_maintainers_keywords {
    Test macports::unobscure_maintainers with nomaintainer and openmaintainer keywords
} -body {
    return [macports::unobscure_maintainers {nomaintainer openmaintainer}]
} -result {{keyword nomaintainer} {keyword openmaintainer}}

test unobscure_maintainers_multiple {
    Test macports::unobscure_maintainers with multiple maintainers
} -body {
    return [macports::unobscure_maintainers {@github-user example.com:localpart}]
} -result {{github github-user} {email localpart@example.com}}

test unobscure_maintainers_multivalue {
    Test macports::unobscure_maintainers with multi-value maintainers
} -body {
    return [macports::unobscure_maintainers {{handle @github-user}}]
} -result {{email handle@macports.org github github-user}}


test ldindex {
    Ldindex unit test.
} -body {
    set list {0 1 2 3}
    if {[ldindex list 1] != 1} {
        return "FAIL: correct element not popped, single index"
    }
    if {$list ne {0 2 3}} {
        return "FAIL: wrong list remaining, single index"
    }
    if {[catch {ldindex list 4}] != 1} {
        return "FAIL: error not detected"
    }
    if {[ldindex list] ne {0 2 3}} {
        return "FAIL: not all elements popped"
    }
    if {$list ne ""} {
        return "FAIL: list not emptied"
    }
    set list {{{0 1} {2 3}} {{4 5} {6 7}}}
    if {[ldindex list 1 1 1] != 7} {
        return "FAIL: correct element not popped, multiple indices"
    }
    if {$list ne {{{0 1} {2 3}} {{4 5} 6}}} {
        return "FAIL: wrong list remaining, multiple indices"
    }
    return "ldindex successful."
} -result "ldindex successful."


test lpop {
    Lpop unit test.
} -body {
    set list {0 1 2}
    if {[lpop list] != 2} {
        return "FAIL: element not popped"
    }
    if {$list != {0 1}} {
        return "FAIL: wrong element popped"
    }
    return "lpop successful."
} -result "lpop successful."


test lpush {
    Lpush unit test.
} -body {
    set list {0 1}
    if {[lpush list 2] != {0 1 2}} {
        return "FAIL: element not appended"
    }
    if {[lpush list 3 4] != {0 1 2 3 4}} {
        return "FAIL: multiple elements not appended"
    }
    if {[lpush list1 0] != 0} {
        return "FAIL: list not created"
    }
    return "lpop successful."
} -result "lpop successful."


test lshift {
    Lshift unit test.
} -body {
    set list {0 1 2}
    set list1 {}

    if {[lshift list] != 0} {
        return "FAIL: wrong element popped"
    }
    if {$list != {1 2}} {
        return "FAIL: wrong list remaining"
    }
    if {[lshift list1] ne ""} {
        return "FAIL: empty list not detected"
    }
    return "lshift successful."
} -result "lshift successful."


test lunshift {
    Lunshift unit test.
} -body {
    set list {3 4}

    if {[lunshift list 2] != {2 3 4}} {
        return "FAIL: element not inserted"
    }
    if {[lunshift list 0 1] != {0 1 2 3 4}} {
        return "FAIL: multiple elements not inserted"
    }
    if {[lunshift list2 0] != 0} {
        return "FAIL: list not created"
    }
    return "lunshift successful."
} -result "lunshift successful."



test throw {
    Throw unit test.
} -setup {
    proc test_proc {arg} {
        catch {throw $arg} res
        return $res
    }
} -body {
    if {[test_proc {7 msg info}] != "error: 7 msg info"} {
        return "FAIL: wrong error returneed"
    }
    if {[catch {test_proc ""}] != 0} {
        return "FAIL: wrong value returned"
    }
    return "throw successful."
} -result "throw successful."


test try {
    Try unit test.
} -setup {
    proc test_proc {} {try {return 7} catch * {return 6}}
    proc test_proc2 {} {try {error "msg" "info" 3} catch * {return 6}}
} -body {
    if {[test_proc] != 7} {
        return "FAIL: try clause not working"
    }
    if {[test_proc2] != 6} {
        return "FAIL: catch clause not working"
    }
    if {[catch {try}] != 1} {
        return "FAIL: no args err not detected"
    }
    return "try successful."
} -result "try successful."


cleanupTests
