Source

ocaml-sip / tests / Test_sip_types.ml

Full commit
(*********************************************************************************************************************
 * Copyrights (C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *  http://bitbucket.org/wieczyk/ocaml-sip
 *)

open Testdata_messages
open Batteries
open OUnit
open Sip_types
open Testdata_messages.Util

(*********************************************************************************************************************
 * Helpers
 *)

let assert_str_equal msg a b =
    assert_equal ~msg:msg ~printer:String.quote a b


let prepare_valid_case  title input output = (title, input, output)
let prepare_valid_case' title output = (String.quote title, title, output)
let prepare_invalid_case title failmsg input = (title, failmsg, input)
let prepare_invalid_case' title failmsg = (String.quote title, failmsg, title)

module type Test_for_type_converters = sig

    type t

    val converters : (string -> t) * (t -> string)

    val valid_data : (string * string * t) list

    val invalid_data : (string * string * string) list

end

(*********************************************************************************************************************
 * SIP version
 *)

module Test_sip_version : Test_for_type_converters = struct

    type t = sip_version

    let converters =
        (sip_version_of_string, string_of_sip_version)

    let valid_data =
        [ prepare_valid_case'
            "SIP/1.0"
            SIP_1_0

        ; prepare_valid_case' 
            "SIP/1.1"
            (UnknownSipVersion "1.1")

        ; prepare_valid_case' 
            "SIP/2.0"
            SIP_2_0

        ; prepare_valid_case'
            "SIP/2.1"
            (UnknownSipVersion "2.1")

        ; prepare_valid_case'
            "SIP/3.0"
            (UnknownSipVersion "3.0")
        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_version_of_string" in
        [ prepare "HTTP/1.0"
        ; prepare "HTTP/1.1"
        ; prepare "HTTP/2.0"
        ; prepare "SIPS/2.0"
        ; prepare "SIPS"
        ; prepare "SIPS/"
        ; prepare "SIP/"
        ; prepare "ELO320"
        ; prepare "fdsfds fsd fsdf sdf s"
        ]

end

(*********************************************************************************************************************
 *)

module Test_sip_request_method : Test_for_type_converters = struct

    type t = sip_request_method

    let converters =
        (sip_request_method_of_string, string_of_sip_request_method)

    let valid_data =
        [ prepare_valid_case'
            "INVITE"
            INVITE

        ; prepare_valid_case'
            "ACK"
            ACK

        ; prepare_valid_case'
            "PRACK"
            PRACK

        ; prepare_valid_case'
            "MESSAGE"
            MESSAGE

        ; prepare_valid_case'
            "REGISTER"
            REGISTER

        ; prepare_valid_case'
            "CANCEL"
            CANCEL

        ; prepare_valid_case'
            "OPTIONS"
            OPTIONS

        ; prepare_valid_case'
            "OPTION"
            (UnknownRequestMethod "OPTION")
        ]

    let invalid_data =
        let prepare title = prepare_invalid_case' title "sip_request_method_of_string" in
        [ prepare "INVITE a"
        ; prepare "a b c"
        ; prepare " INVITE"
        ; prepare " a b c"
        ; prepare "INVITE "
        ]

end

(*********************************************************************************************************************
 *)

(*********************************************************************************************************************
 *)


(*********************************************************************************************************************
 * Test generators
 *)

let valid_conversion_test (input_conv, output_conv) (title, input, output) = 
    "valid conversion -- " ^ title >::
    begin fun () ->
        let input_converted  = input_conv input in
        let output_converted = output_conv output in
        assert_equal
            ~msg:"checking conversion from string"
            output input_converted;
        assert_equal
            ~msg:"checking conversion to string"
            ~printer:String.quote
            input  output_converted
    end

let invalid_conversion_test (input_conv, _)  (title, failmsg, input) =
    "invalid conversion -- " ^ title >::
     begin fun () ->
        let exn = Failure failmsg in
        assert_raises 
            exn (fun () -> input_conv input)
     end

let generate_valid_conversion_tests converters tests =
    List.map (valid_conversion_test converters) tests

let generate_invalid_conversion_tests converters tests =
    List.map (invalid_conversion_test converters) tests

let generate_tests_from_module m = 
    let module M = (val m : Test_for_type_converters) in
    List.concat
        [ generate_valid_conversion_tests M.converters M.valid_data
        ; generate_invalid_conversion_tests M.converters M.invalid_data
        ]

(*********************************************************************************************************************
 * Test suite
 *)

let test_suite = "Types" >::: List.concat
    [ generate_tests_from_module (module Test_sip_version        : Test_for_type_converters )
    ; generate_tests_from_module (module Test_sip_request_method : Test_for_type_converters )
    ]