# Tests for the html module.
#
# This file contains a collection of tests for a module in the
# Standard Tcl Library. Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: html.test,v 1.6 2000/05/31 00:00:00 ericm Exp $

package require tcltest
namespace import -force ::tcltest::*

set myFile [file join [file dirname [info script]] html.tcl]
source $myFile
package require html 1.0

source [file join [file dirname [info script]] ../ncgi/ncgi.tcl]
package require ncgi

test html-1.1 {html::init} {
    html::init
    list [array exists html::defaults] \
	[array size html::defaults] \
	[info exists html::page]
} {1 0 0}

test html-1.2 {html::init} {
    html::init {
	font.face	arial
	body.bgcolor	white
	body.text	black
    }
    lsort [array names html::defaults]
} {body.bgcolor body.text font.face}

test html-1.3 {html::init} {
    catch {html::init wrong num args}
} 1

test html-1.4 {html::init} {
    catch {html::init {wrong num args}}
} 1

test html-2.1 {html::head} {
    catch {html::head}
} 1

test html-2.2 {html::head} {
    html::head "The Title"
} "<html><head>\n\t<title>The Title</title>\n</head>\n"

test html-2.3 {html::head} {
    html::description "The Description"
    html::keywords key word
    html::author "Cathy Coder"
    html::meta metakey metavalue
    html::head "The Title"
} {<html><head>
	<title>The Title</title>
	<!-- Cathy Coder -->
	<meta name="description" value="The Description">
	<meta name="keywords" value="key, word">
	<meta name="metakey" value="metavalue">
</head>
}

test html-3.1 {html::title} {
    catch html::title
} 1

test html-3.2 {html::title} {
    html::title "blah blah"
} "<title>blah blah</title>\n"

test html-4.1 {html::getTitle} {
    html::init
    html::getTitle
} ""

test html-4.2 {html::getTitle} {
    html::init
    html::title "blah blah"
    html::getTitle
} {blah blah}

test html-5.1 {html::meta} {
    html::init
    html::meta one two
} {}

test html-5.2 {html::meta} {
    html::init
    html::meta one two
    lindex $html::page(meta) 0
} {<meta name="one" value="two">}

test html-5.3 {html::meta} {
    html::init
    html::meta one {"one val"}
    lindex $html::page(meta) 0
} {<meta name="one" value="&#34;one val&#34;">}

test html-6.1 {html::keywords} {
    html::init
    html::keywords one two
} {}

test html-6.2 {html::keywords} {
    html::init
    html::keywords one two
    lindex $html::page(meta) 0
} {<meta name="keywords" value="one, two">}

test html-6.3 {html::keywords} {
    html::init
    html::keywords one {"one val"} &
    lindex $html::page(meta) 0
} {<meta name="keywords" value="one, &#34;one val&#34;, &amp;">}

test html-7.1 {html::description} {
    html::init
    html::description "This is the description."
} {}

test html-7.2 {html::description} {
    html::init
    html::description "This is the description."
    lindex $html::page(meta) 0
} {<meta name="description" value="This is the description.">}

test html-7.3 {html::description} {
    html::init
    html::description {one "one val" &}
    lindex $html::page(meta) 0
} {<meta name="description" value="one &#34;one val&#34; &amp;">}

test html-8.1 {html::author} {
    html::init
    html::author "This is the author."
} {}

test html-8.2 {html::author} {
    html::init
    html::author "This is the author."
    set html::page(author)
} {<!-- This is the author. -->
}

test html-8.3 {html::author} {
    html::init
    html::author {one "one val" &}
    set html::page(author)
} {<!-- one "one val" & -->
}

test html-2.1 {html::tagParams} {
    html::init {
	body.bgcolor	red
	font.face	times
    }
    html::tagParam font color="red"
} {color="red" face="times"}

test html-9.1 {html::default} {
    html::init {
	body.bgcolor	red
	font.face	times
    }
    html::default xyzzy
} {}

test html-9.2 {html::default} {
    html::init {
	body.bgcolor	red
	font.face	times
    }
    html::default body.bgcolor
} { bgcolor="red"}

test html-9.3 {html::default} {
    html::init {
	body.bgcolor	red
	font.face	times
    }
    html::default font.face "face=arial"
} {}

test html-9.4 {html::default} {
    html::init {
	body.bgcolor	red
	font.face	times
    }
    html::default font.face "color=blue size=1"
} { face="times"}

test html-10.1 {html::bodyTag} {
    html::init
    html::bodyTag
} {<body>
}

test html-10.2 {html::bodyTag} {
    html::init {
	body.bgcolor	white
	body.text	black
    }
    html::bodyTag
} {<body bgcolor="white" text="black">
}

test html-10.3 {html::bodyTag} {
    html::init {
	body.bgcolor	white
	body.text	black
    }
    html::bodyTag "text=red"
} {<body text=red bgcolor="white">
}

test html-11.1 {html::formValue} {
    ncgi::reset name=value
    ncgi::parse
    html::formValue name
} {name="name" value="value"}

test html-11.2 {html::formValue} {
    ncgi::reset name=value
    ncgi::parse
    html::formValue name2
} {name="name2" value=""}

test html-11.3 {html::formValue} {
    ncgi::reset "name=one+value&name2=%7e"
    ncgi::parse
    html::formValue name2
} {name="name2" value="~"}

test html-12.1 {html::quoteFormValue} {
    html::quoteFormValue name2
} {name2}

test html-12.2 {html::quoteFormValue} {
    html::quoteFormValue {"name2"}
} {&#34;name2&#34;}

test html-12.3 {html::quoteFormValue} {
    html::quoteFormValue {"'><&}
} {&#34;&#39;&gt;&lt;&amp;}

test html-12.4 {html::quoteFormValue} {
    html::quoteFormValue "This is the value."
} {This is the value.}

test html-13.1 {html::textInput} {
    html::init
    ncgi::reset
    ncgi::parse
    html::textInput email
} {<input type="text" name="email" value="">
}

test html-13.2 {html::textInput} {
    html::init
    ncgi::reset email=welch@scriptics.com
    ncgi::parse
    html::textInput email
} {<input type="text" name="email" value="welch@scriptics.com">
}

test html-13.3 {html::textInput} {
    html::init {
	input.size	30
    }
    ncgi::reset
    ncgi::parse
    html::textInput email
} {<input type="text" name="email" value="" size="30">
}

test html-13.4 {html::textInput} {
    html::init {
	input.size	30
    }
    ncgi::reset
    ncgi::parse
    html::textInput email default@foo.com
} {<input type="text" name="email" value="default@foo.com" size="30">
}

test html-13.5 {html::textInput} {
    html::init
    ncgi::reset email=welch@scriptics.com
    ncgi::parse
    html::textInput email default@foo.com
} {<input type="text" name="email" value="welch@scriptics.com">
}

test html-14.1 {html::textInputRow} {
    html::init
    ncgi::reset email=welch@scriptics.com
    ncgi::parse
    html::textInputRow Email email
} {<tr>
	<td>Email</td>
	<td><input type="text" name="email" value="welch@scriptics.com">
</td>
</tr>
}

test html-15.1 {html::passwordInput} {
    html::passwordInput
} {<input type="password" name="password">
}

test html-15.2 {html::passwordInput} {
    html::passwordInput form_pass
} {<input type="password" name="form_pass">
}

test html-16.1 {html::checkbox} {
    ncgi::reset email=welch@scriptics.com
    ncgi::parse
    html::checkbox item value
} {<input type="checkbox" name="item" value="value">
}

test html-16.2 {html::checkbox} {
    ncgi::reset email=welch@scriptics.com
    ncgi::parse
    html::checkbox email value
} {<input type="checkbox" name="email" value="value">
}

test html-17.1 {html::checkValue} {
    ncgi::reset item=xyz
    ncgi::parse
    html::checkbox item xyz
} {<input type="checkbox" name="item" value="xyz" CHECKED>
}

test html-18.1 {html::radioValue} {
    ncgi::reset item=xyz
    ncgi::parse
    html::radioValue item xyz
} {name="item" value="xyz" CHECKED}

test html-19.1 {html::radioSet} {
    ncgi::reset item=2
    ncgi::parse
    html::radioSet item " |\n" {
	One	1
	Two	2
	Three	3
    }
} {<input type="radio" name="item" value="1"> One |
<input type="radio" name="item" value="2" CHECKED> Two |
<input type="radio" name="item" value="3"> Three}

test html-20.1 {html::checkSet} {
    ncgi::reset item=2&item=3+4&x=y
    ncgi::parse
    html::checkSet item " |\n" {
	One	1
	Two	2
	Three	{3 4}
    }
} {<input type="checkbox" name="item" value="1"> One |
<input type="checkbox" name="item" value="2" CHECKED> Two |
<input type="checkbox" name="item" value="3 4" CHECKED> Three}

test html-21.1 {html::select} {
    ncgi::reset item=2&x=y
    ncgi::parse
    html::select item "multiple" {
	One	1
	Two	2
	Three	{3 4}
    }
} {<select name="item" multiple>
<option value="1">One
<option value="2" SELECTED>Two
<option value="3 4">Three
</select>
}

test html-22.1 {html::selectPlain} {
    ncgi::reset item=Three
    ncgi::parse
    html::selectPlain item "" {
	One	Two	Three
    }
} {<select name="item">
<option value="One">One
<option value="Two">Two
<option value="Three" SELECTED>Three
</select>
}

test html-22.2 {html::selectPlain} {
    ncgi::reset item=Three
    ncgi::parse
    html::selectPlain another "" {
	One	Two	Three
    } One
} {<select name="another">
<option value="One" SELECTED>One
<option value="Two">Two
<option value="Three">Three
</select>
}

test html-23.1 {html::textarea} {
    ncgi::reset item=Three
    ncgi::parse
    html::textarea info
} {<textarea name="info"></textarea>
}
test html-23.2 {html::textarea} {
    html::init {
	textarea.cols 50
	textarea.rows 8
    }
    ncgi::reset info=[ncgi::encode "The textarea value."]
    ncgi::parse
    html::textarea info
} {<textarea name="info" cols="50" rows="8">The textarea value.</textarea>
}

test html-24.1 {html::submit} {
    catch {html::submit}
} {1}

test html-24.1 {html::submit} {
    catch {html::submit wrong num args}
} {1}

test html-24.1 {html::submit} {
    html::submit "Push Me"
} {<input type="submit" name="submit" value="Push Me">
}

test html-24.2 {html::submit} {
    html::submit "Push Me" push
} {<input type="submit" name="push" value="Push Me">
}

test html-25.1 {html::varEmpty} {
    catch {html::varEmpty}
} 1
test html-25.2 {html::varEmpty} {
    catch {html::varEmpty wrong num args}
} 1

test html-25.3 {html::varEmpty} {
    if {[info exist x]} {
	unset x
    }
    html::varEmpty x
} 1
test html-25.4 {html::varEmpty} {
    if {[info exist x]} {
	unset x
    }
    set x ""
    html::varEmpty x
} 1

test html-25.3 {html::varEmpty} {
    if {[info exist x]} {
	unset x
    }
    set x "foo"
    html::varEmpty x
} 0

test html-26.1 {html::refresh} {
    catch {html::refresh}
} 1
test html-26.2 {html::refresh} {
    catch {html::refresh wrong num args}
} 1
test html-26.2 {html::refresh} {
    html::refresh 4
} {}
test html-26.3 {html::refresh} {
    html::init
    html::refresh 4
    html::head title
} {<html><head>
	<title>title</title>
	<meta http-equiv="Refresh" content="4">

</head>
}
test html-26.4 {html::refresh} {
    html::init
    html::refresh 9 http://www.scriptics.com
    html::head title
} {<html><head>
	<title>title</title>
	<meta http-equiv="Refresh" content="9; url=http://www.scriptics.com">

</head>
}

