Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
b60fe6e7
J
json
项目概览
畅游知识海洋
/
json
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
json
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b60fe6e7
编写于
12月 28, 2014
作者:
N
Niels
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
- removed old test cases
上级
23006bd5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
9 addition
and
429 deletion
+9
-429
Makefile.am
Makefile.am
+2
-6
test/JSON_test.cc
test/JSON_test.cc
+0
-423
test/JSON_unit.cc
test/JSON_unit.cc
+7
-0
未找到文件。
Makefile.am
浏览文件 @
b60fe6e7
noinst_PROGRAMS
=
json
json
_unit
TESTS
=
./json
./json
_unit
noinst_PROGRAMS
=
json_unit
TESTS
=
./json_unit
FLAGS
=
-Wall
-Wextra
-pedantic
-Weffc
++
-Wcast-align
-Wcast-qual
-Wctor-dtor-privacy
-Wdisabled-optimization
-Wformat
=
2
-Winit-self
-Wmissing-declarations
-Wmissing-include-dirs
-Wold-style-cast
-Woverloaded-virtual
-Wredundant-decls
-Wshadow
-Wsign-conversion
-Wsign-promo
-Wstrict-overflow
=
5
-Wswitch
-Wundef
-Wno-unused
-Wnon-virtual-dtor
-Wreorder
json_SOURCES
=
src/JSON.cc src/JSON.h
test
/JSON_test.cc
json_CXXFLAGS
=
$(FLAGS)
-Weffc
++
-std
=
c++11
json_CPPFLAGS
=
-I
$(top_srcdir)
/src
json_unit_SOURCES
=
src/JSON.cc src/JSON.h
test
/catch.hpp
test
/JSON_unit.cc
json_unit_CXXFLAGS
=
$(FLAGS)
-std
=
c++11
json_unit_CPPFLAGS
=
-I
$(top_srcdir)
/src
-I
$(top_srcdir)
/test
-Dprivate
=
public
...
...
test/JSON_test.cc
已删除
100644 → 0
浏览文件 @
23006bd5
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cassert>
#include <JSON.h>
#include <sstream>
void
test_null
()
{
std
::
cerr
<<
"entering test_null()
\n
"
;
/* a null object */
// construct
JSON
a
,
b
;
// copy assign
b
=
JSON
();
// copy construct
JSON
c
(
a
);
// copy construct
JSON
d
=
a
;
// assign operator
JSON
e
=
JSON
();
// compare
assert
(
a
==
b
);
// type
assert
(
a
.
type
()
==
JSON
::
value_type
::
null
);
// empty and size
assert
(
a
.
size
()
==
0
);
assert
(
a
.
empty
()
==
true
);
// output
std
::
cout
<<
a
<<
'\n'
;
// string represetations
assert
(
a
.
toString
()
==
std
::
string
(
"null"
));
// invalid conversion to int
try
{
int
i
=
0
;
i
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast null to JSON number"
));
}
// invalid conversion to double
try
{
double
f
=
0
;
f
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast null to JSON number"
));
}
// invalid conversion to bool
try
{
bool
b
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast null to JSON Boolean"
));
}
// invalid conversion to string
try
{
std
::
string
s
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast null to JSON string"
));
}
std
::
cerr
<<
"leaving test_null()
\n
"
;
}
void
test_bool
()
{
std
::
cerr
<<
"entering test_bool()
\n
"
;
JSON
True
=
true
;
JSON
False
=
false
;
bool
x
=
True
;
std
::
cerr
<<
"leaving test_bool()
\n
"
;
}
void
test_string
()
{
std
::
cerr
<<
"entering test_string()
\n
"
;
/* a string object */
// construct
JSON
a
=
"object a"
;
JSON
b
;
// copy assign
b
=
JSON
(
"object a"
);
// copy construct
JSON
c
(
a
);
// copy construct
JSON
d
=
a
;
// assign operator
JSON
e
=
JSON
(
""
);
// compare
assert
(
a
==
b
);
// type
assert
(
a
.
type
()
==
JSON
::
value_type
::
string
);
// empty and size
assert
(
a
.
size
()
==
1
);
assert
(
a
.
empty
()
==
false
);
// output
std
::
cout
<<
a
<<
'\n'
;
// string represetations
assert
(
a
.
toString
()
==
std
::
string
(
"
\"
object a
\"
"
));
// invalid conversion to int
try
{
int
i
=
0
;
i
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast string to JSON number"
));
}
// invalid conversion to double
try
{
double
f
=
0
;
f
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast string to JSON number"
));
}
// invalid conversion to bool
try
{
bool
b
=
false
;
b
=
a
;
assert
(
false
);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot cast string to JSON Boolean"
));
}
{
// get payload
std
::
string
*
s1
=
a
.
data
().
string
;
std
::
string
s2
=
a
;
assert
(
*
s1
==
s2
);
}
std
::
cerr
<<
"leaving test_string()
\n
"
;
}
void
test_array
()
{
std
::
cerr
<<
"entering test_array()
\n
"
;
JSON
a
;
a
+=
JSON
();
a
+=
1
;
a
+=
1.0
;
a
+=
true
;
a
+=
"string"
;
// type
assert
(
a
.
type
()
==
JSON
::
value_type
::
array
);
// empty and size
assert
(
a
.
size
()
==
5
);
assert
(
a
.
empty
()
==
false
);
// output
std
::
cout
<<
a
<<
'\n'
;
// check for elements
assert
(
a
[
1
]
==
JSON
(
1
));
assert
(
a
[
2
]
==
JSON
(
1.0
));
assert
(
a
[
3
]
==
JSON
(
true
));
assert
(
a
[
4
]
==
JSON
(
"string"
));
// invalid access to element
try
{
a
[
5
]
=
1
;
// assert(false);
}
catch
(
const
std
::
exception
&
ex
)
{
assert
(
ex
.
what
()
==
std
::
string
(
"cannot access element at index 5"
));
}
// get elements
{
int
i
=
a
[
1
];
double
d
=
a
[
2
];
bool
b
=
a
[
3
];
std
::
string
s
=
a
[
4
];
}
// set elements
a
[
1
]
=
2
;
#ifdef __cplusplus11
// construction from initializer list
JSON
b
=
{
JSON
(),
2
,
1.0
,
true
,
"string"
};
assert
(
a
==
b
);
#endif
// iterators
{
size_t
count
=
0
;
for
(
JSON
::
iterator
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
{
size_t
count
=
0
;
for
(
JSON
::
const_iterator
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
{
size_t
count
=
0
;
for
(
JSON
::
const_iterator
i
=
a
.
cbegin
();
i
!=
a
.
cend
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
#ifdef __cplusplus11
{
size_t
count
=
0
;
for
(
auto
element
:
a
)
{
std
::
cerr
<<
element
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
#endif
{
JSON
::
iterator
i
;
size_t
count
=
0
;
for
(
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
{
JSON
::
const_iterator
i
;
size_t
count
=
0
;
for
(
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
{
JSON
::
const_iterator
i
;
size_t
count
=
0
;
for
(
i
=
a
.
cbegin
();
i
!=
a
.
cend
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
}
{
// get payload
std
::
vector
<
JSON
>*
array
=
a
.
data
().
array
;
assert
(
array
->
size
()
==
a
.
size
());
assert
(
array
->
empty
()
==
a
.
empty
());
}
std
::
cerr
<<
"leaving test_array()
\n
"
;
}
void
test_object
()
{
std
::
cerr
<<
"entering test_object()
\n
"
;
// check find()
{
JSON
o
;
o
[
"foo"
]
=
"bar"
;
JSON
::
iterator
i1
=
o
.
find
(
"foo"
);
assert
(
i1
!=
o
.
end
());
assert
(
i1
.
value
()
==
"bar"
);
assert
(
i1
.
key
()
==
"foo"
);
assert
(
*
i1
==
"bar"
);
JSON
::
iterator
i2
=
o
.
find
(
"baz"
);
assert
(
i2
==
o
.
end
());
JSON
a
;
a
+=
"foo"
;
a
+=
"bar"
;
JSON
::
iterator
i
;
i
=
a
.
find
(
"foo"
);
assert
(
i
==
a
.
end
());
}
std
::
cerr
<<
"leaving test_object()
\n
"
;
}
void
test_streaming
()
{
std
::
cerr
<<
"entering test_streaming()
\n
"
;
// stream text representation into stream
std
::
stringstream
i
;
i
<<
"{
\"
foo
\"
: true,
\"
baz
\"
: [1,2,3,4] }"
;
// create JSON from stream
{
JSON
j
,
k
;
i
>>
j
;
k
<<
i
;
assert
(
j
.
toString
()
==
k
.
toString
());
}
// roundtrip
{
std
::
stringstream
o
;
JSON
j
,
k
;
i
>>
j
;
j
>>
o
;
o
>>
k
;
// assert(j.toString() == k.toString()); (order is not preserved)
}
// check numbers
{
std
::
stringstream
number_stream
;
number_stream
<<
"[0, -1, 1, 1.0, -1.0, 1.0e+1, 1.0e-1, 1.0E+1, 1.0E-1, -1.2345678e-12]"
;
JSON
j
;
j
<<
number_stream
;
}
// check Unicode
{
std
::
stringstream
unicode_stream
;
unicode_stream
<<
"[
\"
öäüÖÄÜß
\"
,
\"
ÀÁÂÃĀĂȦ
\"
,
\"
★☆→➠♥︎♦︎☁︎
\"
]"
;
JSON
j
;
j
<<
unicode_stream
;
}
// check escaped strings
{
std
::
stringstream
escaped_stream
;
escaped_stream
<<
"[
\"\\\"
Hallo
\\\"\"
,
\"
\u0123
\"
]"
;
JSON
j
;
j
<<
escaped_stream
;
}
std
::
cerr
<<
"leaving test_streaming()
\n
"
;
}
int
main
()
{
test_null
();
test_bool
();
test_string
();
test_array
();
test_object
();
test_streaming
();
return
0
;
}
test/JSON_unit.cc
浏览文件 @
b60fe6e7
...
...
@@ -149,6 +149,7 @@ TEST_CASE("array")
CHECK_NOTHROW
(
j
[
21
]
=
5
);
CHECK_THROWS_AS
(
j
.
at
(
21
)
=
5
,
std
::
out_of_range
);
CHECK_THROWS_AS
(
nonarray
+=
2
,
std
::
runtime_error
);
CHECK_THROWS_AS
(
nonarray
.
push_back
(
JSON
(
nullptr
)),
std
::
runtime_error
);
const
JSON
k
=
j
;
CHECK_NOTHROW
(
k
[
21
]);
...
...
@@ -1026,6 +1027,12 @@ TEST_CASE("Parser")
CHECK_THROWS_AS
(
JSON
::
parse
(
"infinity"
),
std
::
invalid_argument
);
}
SECTION
(
"parse from C++ string"
)
{
std
::
string
s
=
"{
\"
foo
\"
: [1,2,true] }"
;
CHECK_NOTHROW
(
JSON
::
parse
(
s
));
}
SECTION
(
"user-defined string literal operator"
)
{
auto
j1
=
"[1,2,3]"
_json
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录