this repo has no description
at trunk 72 lines 2.2 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "gtest/gtest.h" 3 4#include "runtime.h" 5#include "test-utils.h" 6 7namespace py { 8namespace testing { 9 10using UnderWarningsModuleTest = RuntimeFixture; 11 12TEST_F(UnderWarningsModuleTest, ModuleImporting) { 13 ASSERT_FALSE(runFromCStr(runtime_, R"( 14import _warnings 15 )") 16 .isError()); 17 RawObject warnings = mainModuleAt(runtime_, "_warnings"); 18 EXPECT_TRUE(warnings.isModule()); 19} 20 21TEST_F(UnderWarningsModuleTest, WarnDoesNothing) { 22 // TODO(T39431178): _warnings.warn() should actually do things. 23 HandleScope scope(thread_); 24 ASSERT_FALSE(runFromCStr(runtime_, R"( 25import _warnings 26result = _warnings.warn("something went wrong") 27)") 28 .isError()); 29 Object result(&scope, mainModuleAt(runtime_, "result")); 30 EXPECT_TRUE(result.isNoneType()); 31} 32 33TEST_F(UnderWarningsModuleTest, WarnWithNoArgsRaisesTypeError) { 34 EXPECT_TRUE( 35 raisedWithStr(runFromCStr(runtime_, R"( 36import _warnings 37_warnings.warn() 38)"), 39 LayoutId::kTypeError, 40 "'warn' takes min 1 positional arguments but 0 given")); 41} 42 43TEST_F(UnderWarningsModuleTest, WarnWithInvalidCategoryRaisesTypeError) { 44 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, R"( 45import _warnings 46_warnings.warn("warning!", 1234) 47)"), 48 LayoutId::kTypeError, 49 "category must be a Warning subclass")); 50} 51 52TEST_F(UnderWarningsModuleTest, WarnWithLargeStacklevelRaisesOverflowError) { 53 EXPECT_TRUE(raisedWithStr(runFromCStr(runtime_, R"( 54import _warnings 55_warnings.warn("hello", stacklevel=1180591620717411303424) # 2 ** 70 56)"), 57 LayoutId::kOverflowError, 58 "Python int too large to convert to C ssize_t")); 59} 60 61TEST_F(UnderWarningsModuleTest, WarnWithInvalidKwRaisesTypeError) { 62 EXPECT_TRUE( 63 raisedWithStr(runFromCStr(runtime_, R"( 64import _warnings 65_warnings.warn("hello", stack_level=3) 66 )"), 67 LayoutId::kTypeError, 68 "warn() got an unexpected keyword argument 'stack_level'")); 69} 70 71} // namespace testing 72} // namespace py