单元测试支持unicode [英] Unit testing for unicode support

查看:106
本文介绍了单元测试支持unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换为unicode并创建一些单元测试来确保unicode正常工作。

I'm trying to convert to unicode and create some unit tests to ensure that unicode is working.

这是我当前的代码,在mb_detect_encoding( )行,我也不确定它是否是unicode支持的有效测试:

Here is my current code, which fails on the mb_detect_encoding() line, and which I'm also not sure whether it is a valid test of unicode support:

    function testMultiLingualEncodings(){
        // Create this string via a heredoc.
        $original = '
        A good day, World!
Schönen Tag, Welt!
Une bonne journée, tout le monde!
يوم جيد، العالم
좋은 일, 세계!
Một ngày tốt lành, thế giới!
こんにちは、世界!
'; // Contains international characters from utf-8
        $this->assertTrue(mb_detect_encoding($original, 'UTF-8', true) === true); // Fails regardless of whether strict is true or not.
        $returned = query_item("select :multi limit 10", array(':multi'=>$original)); // Select this exact string, parameterized, from the database
        //debug($returned, string_diff($returned, $original));
        $this->assertTrue((bool)$original); // test original isn't null.
        $this->assertTrue((bool)$returned); // Test returned string isn't null.
        $this->assertTrue($original === $returned); // Test original exactly matches returned string
    }

所以mb_detect_encoding()以上字符串不是UTF-8。我也试图将该字符串传递到数据库中,并将其与原始字符串进行比较。我不知道这是否是对数据库连接编码的有效测试。

So mb_detect_encoding() says that the initial string above isn't UTF-8. I'm also trying to pass that string into the database and get it out, and then compare with the original string. I'm not sure whether that is a valid test of the database connection's encoding, however.

所以一般来说,如何创建一个单元测试来支持utf-8 ,并且是上面的方法可以修改以解决这个目标?

So in general, how can I create a unit test for utf-8 support, and is the method above something that can be modified to resolve that goal?

推荐答案

对不起,但这没有意义。您的测试文件以一种格式编码。无论您放入测试字符串中的内容将以与文件相同的方式进行编码。我也不会依赖mb_detect_encoding函数。我们来看下面的字符串:abcde。它可以是ASCII或UTF-8。你无法判断,因为没有特殊的特征。编码是一种如何处理数据的方式。

Sorry but that doesn't make sense. Your test file is encoded in one format. Whatever you put into the test string will be encoded in the same way as the file is. I wouldn't also rely on the mb_detect_encoding function. Let's take following string: "abcde". It can be ASCII or UTF-8. You can't judge because there is no special character. Encoding is a way how you intemperate a data.

//编辑

为了使您的测试工作做到 $ this-> assertTrue(mb_detect_encoding($ original,'UTF-8')==='UTF-8')

To make your test work do $this->assertTrue(mb_detect_encoding($original, 'UTF-8') === 'UTF-8')

这篇关于单元测试支持unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆