"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/packages/encode/base64/_test.pony" (1 Nov 2019, 3649 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Pony source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 use "ponytest"
    2 use "collections"
    3 
    4 actor Main is TestList
    5   new create(env: Env) => PonyTest(env, this)
    6   new make() => None
    7 
    8   fun tag tests(test: PonyTest) =>
    9     test(_TestBase64Encode)
   10     test(_TestBase64Decode)
   11     test(_TestBase64EncodeDecode)
   12     test(_TestBase64Quote)
   13 
   14 class iso _TestBase64Encode is UnitTest
   15   """
   16   Test base64 encoding.
   17   Using test examples from RFC 4648.
   18   """
   19   fun name(): String => "encode/Base64.encode"
   20 
   21   fun apply(h: TestHelper) =>
   22     h.assert_eq[String]("", Base64.encode(""))
   23     h.assert_eq[String]("Zg==", Base64.encode("f"))
   24     h.assert_eq[String]("Zm8=", Base64.encode("fo"))
   25     h.assert_eq[String]("Zm9v", Base64.encode("foo"))
   26     h.assert_eq[String]("Zm9vYg==", Base64.encode("foob"))
   27     h.assert_eq[String]("Zm9vYmE=", Base64.encode("fooba"))
   28     h.assert_eq[String]("Zm9vYmFy", Base64.encode("foobar"))
   29 
   30 class iso _TestBase64Decode is UnitTest
   31   """
   32   Test base64 decoding. Examples with and without padding are tested.
   33   Using test examples from RFC 4648.
   34   """
   35   fun name(): String => "encode/Base64.decode"
   36 
   37   fun apply(h: TestHelper) ? =>
   38     h.assert_eq[String]("", Base64.decode[String iso]("")?)
   39     h.assert_eq[String]("f", Base64.decode[String iso]("Zg==")?)
   40     h.assert_eq[String]("fo", Base64.decode[String iso]("Zm8=")?)
   41     h.assert_eq[String]("foo", Base64.decode[String iso]("Zm9v")?)
   42     h.assert_eq[String]("foob", Base64.decode[String iso]("Zm9vYg==")?)
   43     h.assert_eq[String]("fooba", Base64.decode[String iso]("Zm9vYmE=")?)
   44     h.assert_eq[String]("foobar", Base64.decode[String iso]("Zm9vYmFy")?)
   45 
   46     h.assert_eq[String]("", Base64.decode[String iso]("")?)
   47     h.assert_eq[String]("f", Base64.decode[String iso]("Zg")?)
   48     h.assert_eq[String]("fo", Base64.decode[String iso]("Zm8")?)
   49     h.assert_eq[String]("foo", Base64.decode[String iso]("Zm9v")?)
   50     h.assert_eq[String]("foob", Base64.decode[String iso]("Zm9vYg")?)
   51     h.assert_eq[String]("fooba", Base64.decode[String iso]("Zm9vYmE")?)
   52     h.assert_eq[String]("foobar", Base64.decode[String iso]("Zm9vYmFy")?)
   53 
   54 class iso _TestBase64EncodeDecode is UnitTest
   55   """
   56   Test base64 encoding.
   57   Check encoding then decoding gives back original.
   58   """
   59   fun name(): String => "encode/Base64.encodedecode"
   60 
   61   fun apply(h: TestHelper) ? =>
   62     let src = "Check encoding then decoding gives back original."
   63     let enc = recover val Base64.encode(src) end
   64     let dec = recover val Base64.decode[String iso](enc)? end
   65 
   66     h.assert_eq[String](src, dec)
   67 
   68 class iso _TestBase64Quote is UnitTest
   69   """
   70   Test base64 encoding.
   71   Check encoding then decoding something a bit longer.
   72   """
   73   fun name(): String => "encode/Base64.quote"
   74 
   75   fun apply(h: TestHelper) ? =>
   76     let src =
   77       "Man is distinguished, not only by his reason, but by this singular " +
   78       "passion from other animals, which is a lust of the mind, that by a " +
   79       "perseverance of delight in the continued and indefatigable " +
   80       "generation of knowledge, exceeds the short vehemence of any carnal " +
   81       "pleasure."
   82 
   83     let expect =
   84       "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBie" +
   85       "SB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcy" +
   86       "BhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWd" +
   87       "odCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yg" +
   88       "a25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hb" +
   89       "CBwbGVhc3VyZS4="
   90 
   91     let enc = recover val Base64.encode(src) end
   92     h.assert_eq[String](expect, enc)
   93 
   94     let dec = recover val Base64.decode[String iso](enc)? end
   95     h.assert_eq[String](src, dec)