1

私は PHP で 2 つの関数を書きましたstr_to_utf8()(seems_utf8()これらの関数は、他のコードから借用した部分で構成されています)。現在、私はそれらの単体テストを作成しており、適切な単体テストがあることを確認したいと考えています。私は現在、Facebookから持っているものを取りました:

public function test_str_to_utf8()
{
    // Make sure ASCII characters are ignored
    $this->assertEquals( "this\x01 is a \x7f test string", str_to_utf8( "this\x01 is a \x7f test string" ) );

    // Make sure UTF8 characters are ignored
    $this->assertEquals( "\xc3\x9c \xc3\xbc \xe6\x9d\xb1!", str_to_utf8( "\xc3\x9c \xc3\xbc \xe6\x9d\xb1!" ) );

    // Test long strings
    #str_to_utf8( str_repeat( 'x', 1024 * 1024 ) );
    $this->assertEquals( TRUE, TRUE );

    // Test some invalid UTF8 to see if it is properly fixed
    $input = "\xc3 this has \xe6\x9d some invalid utf8 \xe6";
    $expect = "\xEF\xBF\xBD this has \xEF\xBF\xBD\xEF\xBF\xBD some invalid utf8 \xEF\xBF\xBD";
    $this->assertEquals( $expect, str_to_utf8( $input ) );
}

それらは有効なテストケースですか?

4

1 に答える 1

1

このリソースは、UTF-8 をテストするときに役立ちます。

Latin-1 以外のテキストを使用する場合は、PHP ファイルが UTF-8 として保存されていることを確認するか、事前にエスケープする必要があります。

于 2012-10-23T21:31:04.867 に答える