1// Copyright 2012 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/strings/sys_string_conversions.h" 6 7#import <Foundation/Foundation.h> 8 9#include <string> 10 11#include "base/strings/utf_string_conversions.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14namespace base { 15 16TEST(SysStrings, ConversionsFromNSString) { 17 EXPECT_STREQ("Hello, world!", SysNSStringToUTF8(@"Hello, world!").c_str()); 18 19 // Conversions should be able to handle a NULL value without crashing. 20 EXPECT_STREQ("", SysNSStringToUTF8(nil).c_str()); 21 EXPECT_EQ(std::u16string(), SysNSStringToUTF16(nil)); 22} 23 24std::vector<std::string> GetRoundTripStrings() { 25 return { 26 "Hello, World!", // ASCII / ISO-8859 string (also valid UTF-8) 27 "a\0b", // UTF-8 with embedded NUL byte 28 "λf", // lowercase lambda + 'f' 29 "χρώμιο", // "chromium" in greek 30 "כרום", // "chromium" in hebrew 31 "クロム", // "chromium" in japanese 32 33 // Tarot card symbol "the morning", which is outside of the BMP and is not 34 // representable with one UTF-16 code unit. 35 "", 36 }; 37} 38 39TEST(SysStrings, RoundTripsFromUTF8) { 40 for (const auto& string8 : GetRoundTripStrings()) { 41 NSString* nsstring8 = SysUTF8ToNSString(string8); 42 std::string back8 = SysNSStringToUTF8(nsstring8); 43 EXPECT_EQ(string8, back8); 44 } 45} 46 47TEST(SysStrings, RoundTripsFromUTF16) { 48 for (const auto& string8 : GetRoundTripStrings()) { 49 std::u16string string16 = base::UTF8ToUTF16(string8); 50 NSString* nsstring16 = SysUTF16ToNSString(string16); 51 std::u16string back16 = SysNSStringToUTF16(nsstring16); 52 EXPECT_EQ(string16, back16); 53 } 54} 55 56} // namespace base 57