在最近关于存根的问题中,许多答案建议使用 C# 接口或委托来实现存根,但一个答案建议使用条件编译,在生产代码中保留静态绑定。这个答案在阅读时被修改为-2,所以至少有 2 人确实认为这是一个错误的答案。也许滥用 DEBUG 是原因,或者可能使用固定值而不是更广泛的验证。但我不禁想知道:
使用条件编译是否是实现单元测试存根的不适当技术?有时?总是?
谢谢。
编辑-添加: 我想添加一个示例作为实验:
class Foo {
public Foo() { .. }
private DateTime Now {
get {
#if UNITTEST_Foo
return Stub_DateTime.Now;
#else
return DateTime.Now;
#endif
}
}
// .. rest of Foo members
}
Run Code Online (Sandbox Code Playgroud)
相比于
interface IDateTimeStrategy {
DateTime Now { get; }
}
class ProductionDateTimeStrategy : IDateTimeStrategy {
public DateTime Now { get { return DateTime.Now; } }
}
class Foo {
public Foo() : Foo(new ProductionDateTimeStrategy()) …Run Code Online (Sandbox Code Playgroud) Windows仅向Windows Vista提供GetTickCount,并从该操作系统开始提供GetTickCount64.如何通过调用不同的函数来编译C程序?
如何让C编译器检查是否在包含的头文件中声明了一个函数,并根据该特定函数是否可用来编译代码的不同部分?
#if ??????????????????????????????
unsigned long long get_tick_count(void) { return GetTickCount64(); }
#else
unsigned long long get_tick_count(void) { return GetTickCount(); }
#endif
Run Code Online (Sandbox Code Playgroud)
寻找工作样本文件而不仅仅是提示.
编辑:我在(64位)Windows 7 RC上使用minGW中的gcc 3.4.5尝试了以下操作,但它没有帮助.如果这是MinGW问题,我该如何解决这个问题?
#include <windows.h>
#if (WINVER >= 0x0600)
unsigned long long get_tick_count(void) { return 600/*GetTickCount64()*/; }
#else
unsigned long long get_tick_count(void) { return 0/*GetTickCount()*/; }
#endif
Run Code Online (Sandbox Code Playgroud) 我希望能够使用条件编译符号(NUGET或类似符号)打包我的 nuget 文件,以便我可以使用专门用于我的 nuget 构建的预处理器指令。
例如
#if NUGET
[Obsolete("use v2.IMyContract", true)]
#endif
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将其传递给nuget pack MyProject.csproj命令吗?
我有一个包含插件合约的程序集,这些合约有多个版本(程序集中的 .vX. 命名空间),需要由实际实现构建和引用以实现向后兼容性。
我希望能够分发具有破坏[Obsolete(true)]属性的这些合同,以强制消费者在升级他们的 nuget 包时使用最新版本的合同。但是在内部我仍然需要使用这些对象来保持我的向后兼容性。
我尝试从 Cython pyx 文件有条件地生成 C 代码。我的文档用Cython,我可以使用在发现DEF来定义的值,并IF有条件地生成基于定义的值代码,但如何可以设置为从所述值setup.py通过Extension从setuptools。
谢谢你
我正在编写一个 C# 项目,其中具有以下目录结构:
LibFoo
|
---- LibFoo.Shared
| |
| ---- [a bunch of .cs files]
|
---- LibFoo.Uwp
| |
| ---- LibFoo.Uwp.csproj
|
---- LibFoo.Wpf
|
---- LibFoo.Wpf.csproj
Run Code Online (Sandbox Code Playgroud)
我很想知道,是否可以包含共享目录中的 C# 文件,以便它们显示在 Visual Studio 的解决方案资源管理器中?我知道您可以通过设置标签的Link属性来做到这一点<Compile>,但是当.cs项目中的文件数量可变时,我不太确定如何做到这一点。
澄清一下,这是我的 csproj 文件的相关部分:
<PropertyGroup>
<!-- Compile everything in this dir -->
<CompileRoot>..\LibFoo.Shared</CompileRoot>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CompileRoot)\**\*.cs">
<Link> <!--What goes here?--> </Link>
</Compile>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
编辑:忘了提及,它们包含在父目录中这一事实是相关的,因为这就是它们没有出现在 Visual Studio 中的原因。如果它们出现在 VS 中,我就不必执行任何这些链接操作。
编辑2:按照shamp00的建议,我试过这个:
<ItemGroup>
<Compile Include="$(CompileRoot)\**\*.cs"> …Run Code Online (Sandbox Code Playgroud) 我想使用#![feature(custom_test_frameworks)],但如果可能,只能通过 有条件地启用它#[cfg(not(target_os = "custom_os_name"))]。我仍然希望可以选择使用 rusts libtest 在我的主机系统上直接运行一些测试,但显然我不允许通过cfg以下方式修改功能:
错误信息:
note: inner attributes, like `#![no_std]`, annotate the item enclosing them,
and are usually found at the beginning of source files. Outer attributes, like
`#[test]`, annotate the item following them.
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以有条件地启用“内部属性”?
我有一个带有如下命令的 Makefile:
#Makefile
hello:
echo 'hello'
echo $(TAG)
ifdef TAG
$(warning MYWARNING)
else
$(error MYERROR)
endif
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
# make TAG='1.0' hello
Run Code Online (Sandbox Code Playgroud)
我希望该命令执行 echo 'hello',然后 echo $(TAG) 和 $(warning MYWARNING) 但我得到:
Makefile:17: MYWARNING
Makefile:19: *** MYERROR. Stop.
Run Code Online (Sandbox Code Playgroud)
怎么了?
我正在开始开发 2 个应用程序扩展,我想按如下方式设置我的构建:
目前,我对每个项目都有不同的构建配置,以显示和隐藏应用程序的其他方面(在某些情况下也包括编译器标志)。我使用 fastlane 来进行构建。
据我所知,添加或删除应用程序扩展的唯一方法是:
有人能想出更好的解决方案来有条件地包含应用程序扩展吗?
xcode conditional-compilation xcodebuild ios-app-extension fastlane
我正在尝试简化大量遗留C代码,其中,即使在今天,在构建维护它的人之前需要一个源文件,并在编译之前根据各种类型的环境手动修改以下部分.
这个例子如下,但这是问题所在.我的C上生锈了,但我记得不鼓励使用#ifdef.你们能提供更好的选择吗?另外 - 我认为其中一些(如果不是全部)可以设置为环境变量或作为参数传入,如果是这样 - 什么是定义这些然后从源代码访问的好方法?
这是我正在处理的代码片段
#define DAN NO
#define UNIX NO
#define LINUX YES
#define WINDOWS_ES NO
#define WINDOWS_RB NO
/* Later in the code */
#if ((DAN==1) || (UNIX==YES))
#include <sys/param.h>
#endif
#if ((WINDOWS_ES==YES) || (WINDOWS_RB==YES) || (WINDOWS_TIES==YES))
#include <param.h>
#include <io.h>
#include <ctype.h>
#endif
/* And totally insane harcoded paths */
#if (DAN==YES)
char MasterSkipFile[MAXSTR] = "/home/dp120728/tools/testarea/test/MasterSkipFile";
#endif
#if (UNIX==YES)
char MasterSkipFile[MAXSTR] = "/home/tregrp/tre1/tretools/MasterSkipFile";
#endif
#if (LINUX==YES)
char MasterSkipFile[MAXSTR] = "/ptehome/tregrp/tre1/tretools/MasterSkipFile";
#endif …Run Code Online (Sandbox Code Playgroud) 是否可以#define在"if"语句中使用?以下代码有效,但我收到一条警告,即宏正在重新定义.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
#define TableViewHeight 916
#define DisplayHeight 1024
#define DisplayWidth 768
}
else {
#define TableViewHeight 374
#define DisplayHeight 480
#define DisplayWidth 320
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个,但它没有用:
#ifdef UIUserInterfaceIdiomPad
#define TableViewHeight 916
#define DisplayHeight 1024
#define DisplayWidth 768
#else
#define TableViewHeight 374
#define DisplayHeight 480
#define DisplayWidth 320
#endif
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
c ×2
.net ×1
bash ×1
c# ×1
cython ×1
fastlane ×1
hardcoded ×1
ios ×1
macros ×1
makefile ×1
msbuild ×1
nuget ×1
objective-c ×1
python ×1
rust ×1
setuptools ×1
stub ×1
unit-testing ×1
xcode ×1
xcodebuild ×1