Skip to content

Commit c29e48f

Browse files
committed
Added setters for fox environment variables.
Setting environment variables will override the setter values, but this will provide a work around to xcodebuild test not passing environment variables to the iOS simulator. Fixes #20.
1 parent d5b0a9c commit c29e48f

File tree

3 files changed

+140
-23
lines changed

3 files changed

+140
-23
lines changed

Fox/Public/FOXEnvironment.h

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#import "FOXMacros.h"
22

33
/*! The default number of tests (which is 500).
4-
* Can be overridden by FOX_NUM_TESTS environment variables.
54
*
5+
* Can be overridden by FOX_NUM_TESTS environment variables or by
6+
* FOXSetNumberOfTests(). The environment variable superseeds the setter.
7+
*
8+
* The setter is provided since iOS test bundles cannot be given environment
9+
* variables via the xcodebuild test command.
10+
*
11+
* @seealso FOXSetNumberOfTests to use the setter.
612
* @returns the number of tests the default runner should create.
713
*/
814
FOX_EXPORT NSUInteger FOXGetNumberOfTests(void);
915

1016
/*! The default maximum size of data generation for tests (default 200).
11-
* Can be overridden by FOX_MAX_SIZE environment variable.
17+
*
18+
* Can be overridden by FOX_MAX_SIZE environment variable or by
19+
* FOXSetMaximumSize(). The environment variable superseeds the setter.
1220
*
1321
* The maximum size factor that generators will generate. A size value is generated
1422
* by FOX as the size the generators in the properties use. The maximum size factor
@@ -19,15 +27,51 @@ FOX_EXPORT NSUInteger FOXGetNumberOfTests(void);
1927
* the size indicates how large each element is (eg - large integers) AND how many
2028
* elements are generated.
2129
*
30+
* The setter is provided since iOS test bundles cannot be given environment
31+
* variables via the xcodebuild test command.
32+
*
33+
* @seealso FOXSetMaximumSize
2234
* @returns the maximum size the default runner can generate for data in tests.
2335
*/
2436
FOX_EXPORT NSUInteger FOXGetMaximumSize(void);
2537

2638
/*! The default seed Fox uses to create data and run tests.
27-
* Can be overridden by FOX_SEED environment variable.
39+
*
40+
* Can be overridden by FOX_SEED environment variable or by FOXSetSeed. The
41+
* environment variable superseeds the setter.
2842
*
2943
* The default will be the current time (using time(NULL)).
3044
*
45+
* The setter is provided since iOS test bundles cannot be given environment
46+
* variables via the xcodebuild test command.
47+
*
3148
* @returns the random seed the default runner will use.
3249
*/
3350
FOX_EXPORT NSUInteger FOXGetSeed(void);
51+
52+
/*! Sets the default number of tests to use.
53+
*
54+
* Does not override setting the FOX_NUM_TESTS environment variable.
55+
*
56+
* @param defaultNumberOfTests The number of tests to generate by default.
57+
* @seealso FOXGetNumberOfTests
58+
*/
59+
FOX_EXPORT void FOXSetNumberOfTests(NSUInteger defaultNumberOfTests);
60+
61+
/*! Sets the default maximum size to use.
62+
*
63+
* Does not override setting the FOX_MAX_SIZE environment variable.
64+
*
65+
* @param defaultMaximumSize The maximum size hint Fox should use to generate data by default.
66+
* @seealso FOXGetMaximumSize
67+
*/
68+
FOX_EXPORT void FOXSetMaximumSize(NSUInteger defaultMaximumSize);
69+
70+
/*! Sets the default random seed to use.
71+
*
72+
* Does not override setting the FOX_SEED environment variable.
73+
*
74+
* @param defaultRandomSeed The random seed to use when generating data by default.
75+
* @seealso FOXGetSeed
76+
*/
77+
FOX_EXPORT void FOXSetSeed(NSUInteger defaultRandomSeed);

Fox/Public/FOXEnvironment.m

+37-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#import "FOXEnvironment.h"
22

3-
43
const NSUInteger FOXDefaultNumberOfTests = 500;
54
const NSUInteger FOXDefaultMaximumSize = 200;
65

6+
static NSUInteger __FOXNumberOfTests = FOXDefaultNumberOfTests;
7+
static NSUInteger __FOXMaximumSize = FOXDefaultMaximumSize;
8+
static NSUInteger __FOXSeed = 0;
9+
710
static NSUInteger FOXGetUIntegerFromEnv(const char *envname, NSUInteger defaultValue) {
811
const char *envval = getenv(envname);
912
unsigned long number = defaultValue;
@@ -13,18 +16,42 @@ static NSUInteger FOXGetUIntegerFromEnv(const char *envname, NSUInteger defaultV
1316
return (NSUInteger)number;
1417
}
1518

16-
FOX_EXPORT NSUInteger FOXGetNumberOfTests(void) {
17-
return FOXGetUIntegerFromEnv("FOX_NUM_TESTS", FOXDefaultNumberOfTests);
19+
static NSUInteger FOXEnsureNonZero(NSUInteger value, NSUInteger defaultValue) {
20+
if (value == 0) {
21+
return defaultValue;
22+
} else {
23+
return value;
24+
}
25+
}
26+
27+
NSUInteger FOXGetNumberOfTests(void) {
28+
return FOXEnsureNonZero(FOXGetUIntegerFromEnv("FOX_NUM_TESTS", __FOXNumberOfTests),
29+
FOXDefaultNumberOfTests);
30+
}
31+
32+
NSUInteger FOXGetMaximumSize(void) {
33+
return FOXEnsureNonZero(FOXGetUIntegerFromEnv("FOX_MAX_SIZE", __FOXMaximumSize),
34+
FOXDefaultNumberOfTests);
35+
}
36+
37+
NSUInteger FOXGetSeed(void) {
38+
if (!__FOXSeed) {
39+
__FOXSeed = (NSUInteger)time(NULL);
40+
}
41+
return FOXGetUIntegerFromEnv("FOX_SEED", __FOXSeed);
42+
}
43+
44+
void FOXSetNumberOfTests(NSUInteger defaultNumberOfTests) {
45+
__FOXNumberOfTests = defaultNumberOfTests;
1846
}
1947

20-
FOX_EXPORT NSUInteger FOXGetMaximumSize(void) {
21-
return FOXGetUIntegerFromEnv("FOX_MAX_SIZE", FOXDefaultMaximumSize);
48+
void FOXSetMaximumSize(NSUInteger defaultMaximumSize) {
49+
__FOXMaximumSize = defaultMaximumSize;
2250
}
2351

24-
FOX_EXPORT NSUInteger FOXGetSeed(void) {
25-
static NSUInteger ___seed;
26-
if (!___seed) {
27-
___seed = FOXGetUIntegerFromEnv("FOX_SEED", (NSUInteger)time(NULL));
52+
void FOXSetSeed(NSUInteger defaultRandomSeed) {
53+
if (defaultRandomSeed == 0) {
54+
defaultRandomSeed = (NSUInteger)time(NULL);
2855
}
29-
return ___seed;
56+
__FOXSeed = defaultRandomSeed;
3057
}

docs/source/runner.rst

+56-10
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ three parameters to configure Fox's test generation:
2323

2424
- The **seed** allows you to seed the random number generator Fox uses. This
2525
allows you to set the PRNG to help reproduce failures that Fox may have
26-
generated during a test run. Setting this to the default (``0``) will make
26+
generated during a test run. Setting this to the default (``0``) makes
2727
Fox generate a seed based on the current time.
28-
- The **number of tests** indicates the number of tests Fox will generate for
28+
- The **number of tests** indicates the number of tests Fox generates for
2929
this particular property. More tests generated will more thoroughly cover the
30-
property at the cost of time. Setting this to the default (``0``) will make
30+
property at the cost of time. Setting this to the default (``0``) makes
3131
Fox run ``500`` tests.
32-
- The **maximum size** indicates the maximum size factor Fox will use when
32+
- The **maximum size** indicates the maximum size factor Fox uses when
3333
generating tests. Generators use this size factor as a hint to produce data
34-
of the appropriate sizes. For example, ``FOXInteger`` will generate integers
35-
within the range of 0 to ``maximumSize`` and ``FOXArray`` will generate
34+
of the appropriate sizes. For example, ``FOXInteger`` generates integers
35+
within the range of 0 to ``maximumSize`` and ``FOXArray`` generates
3636
arrays whose number of elements are in the range of 0 to ``maximumSize``.
37-
Setting this to the default (``0``) will make Fox run with a ``maximumSize``
37+
Setting this to the default (``0``) makes Fox run with a ``maximumSize``
3838
of ``200``. If you know this property's data generation can tolerate larger
3939
sizes, feel free to increase this. Large collection generation can be
4040
prohibitively expensive.
@@ -45,8 +45,8 @@ be recorded to reproduce a failure that Fox may have generated.
4545
Per Assertion Configuration
4646
---------------------------
4747

48-
By default, Fox will generate **500 tests per assertion** with a **maximum size
49-
of 200** and a random seed. By changing ``FOXAssert`` to ``FOXAssertWithOptions``
48+
By default, Fox generates **500 tests per assertion** with a **maximum size of
49+
200** and a random seed. By changing ``FOXAssert`` to ``FOXAssertWithOptions``
5050
we can provide optional configuration by using the ``FOXOptions``::
5151

5252
FOXAssertWithOptions(FOXForAll(...), (FOXOptions){
@@ -61,14 +61,60 @@ Global Configuration
6161
Values can be overridden using `environment variables`_ to globally change the
6262
defaults.
6363

64+
.. note:: Note that as of this time of writing, ``xcodebuild test`` (command
65+
line) does not properly pass environment variables to test bundles
66+
for iOS. Use the setter style listed after the environment variables
67+
instead.
68+
69+
It is fine to set environment variables via Xcode.
70+
6471
- Setting ``FOX_SEED`` can specify a specific seed to run for all properties.
6572
- Setting ``FOX_NUM_TESTS`` sets the number of tests to generate for each
6673
property.
67-
- Setting ``FOX_MAX_SIZE`` sets the maximum size factor Fox will use to when
74+
- Setting ``FOX_MAX_SIZE`` sets the maximum size factor Fox uses to when
6875
generating tests.
6976

7077
.. _environment variables: http://nshipster.com/launch-arguments-and-environment-variables/
7178

79+
If you cannot use environment variables, Fox also allows you to manually
80+
override the values via setters:
81+
82+
- ``FOXSetSeed(NSUInteger seed)`` sets the random seed.
83+
- ``FOXSetNumberOfTests(NSUInteger numTests)`` sets the number of tests.
84+
- ``FOXSetMaximumSize(NSUInteger maxSize)`` sets the maximum size.
85+
86+
An easy way to use these setters would be in an ``+[initialize]`` or ``+[load]`` method::
87+
88+
@interface TestHelper : NSObject
89+
@end
90+
91+
@implementation TestHelper
92+
93+
+ (void)initialize {
94+
if (self == [TestHelper class]) {
95+
FOXSetNumberOfTests(200);
96+
FOXSetMaximumSize(50);
97+
}
98+
}
99+
100+
@end
101+
102+
Using compile-time settings or macros, you can conditionally customize the
103+
values as needed.
104+
105+
Configuration Priority
106+
----------------------
107+
108+
Even though Fox accepts many different ways of setting global configuration,
109+
there is a specific ordering Fox checks for configuration values.
110+
111+
The priority that Fox takes for these configurations are in order:
112+
113+
1. The per-assertion configuration if provided.
114+
2. The environment variable overrides if provided.
115+
3. The setter function values are used if provided.
116+
4. Fox's internal default values are used.
117+
72118
.. _Random Number Generators:
73119

74120
Random Number Generators

0 commit comments

Comments
 (0)